Java泛型07 : Java自带的泛型Collection、List、Map、Set以及工具类Arrays和Collections
程序员文章站
2024-03-23 13:24:58
...
本章主要对Java自带的泛型Collection、List、Map和Set进行简单示例。
1.Java集合
首先回顾一下常用的Java集合类型的相关接口和类之间的关系:
Java在集合类型的定义中使用了大量的泛型,下面对这些常见的类型进行学习。
2.List和Set
示例代码:
/**
* 演示Java自带泛型的集合Collection类型
* Created by 韩超 on 2018/1/30.
*/
public class CollectionPrintUtils {
private final static Logger LOGGER = Logger.getLogger(CollectionPrintUtils.class);
/**
* <p>Title: 打印集合</p>
* @author 韩超@bksx 2018/1/31 13:31
*/
public static <E extends Collection> void pringCollection(E collection){
if(null == collection || collection.isEmpty()) {
LOGGER.info("无记录");
}else {
//java lambda表达式
collection.forEach(LOGGER::info);
}
}
/**
* <p>Java泛型在List和Set中的使用</p>
* @author hanchao 2018/2/23 22:13
**/
public static void main(String[] args) {
//List
List<Integer> integerList = new ArrayList<Integer>();
integerList.add(new Integer(1));
integerList.add(2);
integerList.add(new Integer(3));
LOGGER.info("integerList的元素如下:");
CollectionPrintUtils.pringCollection(integerList);
//Set
Set<String > stringSet = new HashSet<String>();
stringSet.add("set1");
stringSet.add("set2");
stringSet.add("set3");
LOGGER.info("stringSet的元素如下");
CollectionPrintUtils.pringCollection(stringSet);
}
}
运行结果:
2018-02-23 22:57:44 INFO CollectionPrintUtils:37 - integerList的元素如下:
2018-02-23 22:57:44 INFO CollectionPrintUtils:1249 - 1
2018-02-23 22:57:44 INFO CollectionPrintUtils:1249 - 2
2018-02-23 22:57:44 INFO CollectionPrintUtils:1249 - 3
2018-02-23 22:57:44 INFO CollectionPrintUtils:44 - stringSet的元素如下
2018-02-23 22:57:44 INFO CollectionPrintUtils:75 - set3
2018-02-23 22:57:44 INFO CollectionPrintUtils:75 - set2
2018-02-23 22:57:44 INFO CollectionPrintUtils:75 - set1
3.Map
示例代码:
/**
* 演示Java自带泛型的集合Map类型
* Created by 韩超 on 2018/1/31.
*/
public class MapPrintUtils {
private final static Logger LOGGER = Logger.getLogger(MapPrintUtils.class);
/**
* <p>Title: 打印Map</p>
* @author 韩超@bksx 2018/1/31 14:28
*/
public static <E extends Map> void printSet(E map){
if (null == map || map.isEmpty()){
LOGGER.info("当前Map无值!");
}else {
for (Object key : map.keySet()){
Object value = map.get(key);
LOGGER.info("key:" + key + ",value:" + value);
}
}
}
/**
* <p>Java泛型在Map中的使用</p>
* @author hanchao 2018/2/23 22:14
**/
public static void main(String[] args) {
//hashMap
Map<String ,String> hashMap = new HashMap<>();
hashMap.put("key1","value1");
hashMap.put(null,"value2");
hashMap.put("key3",null);
LOGGER.info("hashMap的元素如下");
MapPrintUtils.printSet(hashMap);
//hashTable
Map<Integer,Double> hashTable = new Hashtable<>();
hashTable.put(1,2D);
hashTable.put(2,3D);
hashTable.put(3,4D);
LOGGER.info("hashTable的元素如下:");
MapPrintUtils.printSet(hashTable);
}
}
运行结果:
2018-02-23 22:58:35 INFO MapPrintUtils:40 - hashMap的元素如下
2018-02-23 22:58:35 INFO MapPrintUtils:26 - key:key1,value:value1
2018-02-23 22:58:35 INFO MapPrintUtils:26 - key:null,value:value2
2018-02-23 22:58:35 INFO MapPrintUtils:26 - key:key3,value:null
2018-02-23 22:58:35 INFO MapPrintUtils:48 - hashTable的元素如下:
2018-02-23 22:58:35 INFO MapPrintUtils:26 - key:3,value:4.0
2018-02-23 22:58:35 INFO MapPrintUtils:26 - key:2,value:3.0
2018-02-23 22:58:35 INFO MapPrintUtils:26 - key:1,value:2.0
4.Arrays和Collections
示例代码:
/**
* <p>集合工具类Collections和Arrays中的泛型示例</p>
* @author hanchao 2018/2/23 22:33
**/
public class ArraysAndCollectionsUtils {
private static final Logger LOGGER = Logger.getLogger(ArraysAndCollectionsUtils.class);
public static void main(String[] args) {
LOGGER.info("Arrays工具类中的泛型示例:public static <T> List<T> asList(T... a) {}");
List<? extends Number> numberList = Arrays.asList(1,2D,3L,4.0F);
numberList.forEach(LOGGER::info);
System.out.println();
LOGGER.info("Collections工具类中的泛型示例:二分查找public static <T>" +
" int binarySearch(List<? extends Comparable<? super T>> list, T key) {}");
List<Integer> integerList = new ArrayList<>();
integerList.add(1);
integerList.add(2);
integerList.add(3);
integerList.add(4);
LOGGER.info("二分查找:" + Collections.binarySearch(integerList,3));
System.out.println();
LOGGER.info("Collections工具类中的泛型示例:排序public static <T extends Comparable<? super T>> void sort(List<T> list) {}");
List<Double> doubleList = Arrays.asList(2D,1D,3D);
LOGGER.info("第一次打印:");
doubleList.forEach(LOGGER::info);
LOGGER.info("排序");
Collections.sort(doubleList);
LOGGER.info("第二次打印:");
doubleList.forEach(LOGGER::info);
System.out.println();
LOGGER.info("Collections工具类中的泛型示例:排序public static <T> void copy(List<? super T> dest, List<? extends T> src) {}");
List<String > stringList = Arrays.asList("A","B","C");
List<String > stringList2 = Arrays.asList("a","b","c","d");
Collections.copy(stringList2,stringList);
LOGGER.info(stringList2);
}
}
运行结果:
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:18 - Arrays工具类中的泛型示例:public static <T> List<T> asList(T... a) {}
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 1
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 2.0
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 3
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 4.0
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:23 - Collections工具类中的泛型示例:二分查找public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) {}
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:30 - 二分查找:2
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:33 - Collections工具类中的泛型示例:排序public static <T extends Comparable<? super T>> void sort(List<T> list) {}
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:35 - 第一次打印:
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 2.0
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 1.0
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 3.0
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:37 - 排序
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:39 - 第二次打印:
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 1.0
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 2.0
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:3880 - 3.0
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:43 - Collections工具类中的泛型示例:排序public static <T> void copy(List<? super T> dest, List<? extends T> src) {}
2018-02-23 23:00:02 INFO ArraysAndCollectionsUtils:47 - [A, B, C, d]
5.总结
由于这些集合类和工具类提供的静态方法都比较常见,上面没有过多描述。
其实在Java集合框架中,还有更多的泛型类和泛型方法,这里就不一一例句了。