Java泛型07 : Java自带的泛型Collection、List、Map、( 二 )


* @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 List asList(T... a) {}");List numberList = Arrays.asList(1,2D,3L,4.0F);numberList.forEach(LOGGER::info);System.out.println();LOGGER.info("Collections工具类中的泛型示例:二分查找public static " +" int binarySearch(List> list, T key) {}");List 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 > void sort(List list) {}");List 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 void copy(List dest, List src) {}");List stringList = Arrays.asList("A","B","C");List stringList2 = Arrays.asList("a","b","c","d");Collections.copy(stringList2,stringList);LOGGER.info(stringList2);}}
运行结果:
2018-02-23 23:00:02 INFOArraysAndCollectionsUtils:18 - Arrays工具类中的泛型示例:public static List asList(T... a) {}2018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 12018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 2.02018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 32018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 4.02018-02-23 23:00:02 INFOArraysAndCollectionsUtils:23 - Collections工具类中的泛型示例:二分查找public static int binarySearch(List> list, T key) {}2018-02-23 23:00:02 INFOArraysAndCollectionsUtils:30 - 二分查找:22018-02-23 23:00:02 INFOArraysAndCollectionsUtils:33 - Collections工具类中的泛型示例:排序public static > void sort(List list) {}2018-02-23 23:00:02 INFOArraysAndCollectionsUtils:35 - 第一次打印:2018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 2.02018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 1.02018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 3.02018-02-23 23:00:02 INFOArraysAndCollectionsUtils:37 - 排序2018-02-23 23:00:02 INFOArraysAndCollectionsUtils:39 - 第二次打印:2018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 1.02018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 2.02018-02-23 23:00:02 INFOArraysAndCollectionsUtils:3880 - 3.02018-02-23 23:00:02 INFOArraysAndCollectionsUtils:43 - Collections工具类中的泛型示例:排序public static void copy(List dest, List src) {}2018-02-23 23:00:02 INFOArraysAndCollectionsUtils:47 - [A, B, C, d]
5.总结
由于这些集合类和工具类提供的静态方法都比较常见 , 上面没有过多描述 。
其实在Java集合框架中 , 还有更多的泛型类和泛型方法 , 这里就不一一例句了 。