Collections 类
Collections 则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序、搜索以及线程安全等各种操作
常用方法
public static <T extends Comparable<? super T>> void sort(List list)
根据元素的自然顺序对指定列表按升序进行排序
参数:list - 要排序的列表
public static void shuffle(List<?> list)
随机排序
参数:list - 要改组的列表
public static int binarySearch(List<? extends Comparable<? super T>> list,T key)
使用二分搜索法搜索指定列表,以获得指定对象
参数:list - 要搜索的列表、key - 要搜索的值
public static void reverse(List<?> list)
反转指定列表中元素的顺序
参数:list - 元素要被反转的列表
public static boolean addAll(Collection<? super T> c,T… elements)
将所有指定元素添加到指定 collection 中
参数:c - 要插入 elements 的 collection,elements - 插入 c 的元素
例如:
ArrayList<String> a1 = new ArrayList<String>();
ArrayList<String> a2 = new ArrayList<String>();
a1.add("学");
a1.add("好");
a1.add("Java");
a2.add("做");
a2.add("好");
a2.add("程序");
Collections.copy(a2, a1);
System.out.println(a2);
public static T max(Collection<? extends T> coll)
public static T min(Collection<? extends T> coll)
求最大最小数
参数:coll - 将确定其最小元素的 collection
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)