Collectons集合工具类的简单使用
public class Demo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("xl");
list.add("xxl");
list.add("xxxl");
list.add("xxxxl");
//二分查找法
//Collections.binarySearch(list,"xxl");//需要先排序
//如果集合中是自定义数据类型 或者该类型无法实现比较器的 需要用到Comparator比较器
//Collections.binarySearch(list,"xxl", Comparator);//最后面的参数是比较器
//System.out.println("list = " + list);
ArrayList<String> dest = new ArrayList<>();
//必须首先保证目标集合有元素 而且元素数量不小于原集合
//Collections.copy(dest,list);
//System.out.println("dest = " + dest);//.IndexOutOfBoundsException: Source does not fit in dest
//dest.add("oo");
//dest.add("ooo");
//dest.add("oooo");
//dest.add("ooooo");
//Collections.copy(dest,list);
//System.out.println("dest = " + dest);//dest = [xl, xxl, xxxl, xxxxl]
//比较两个集合是否有相同元素,没有相同元素则返回true
//System.out.println(Collections.disjoint(list,dest));
//用枚举器遍历Collection
/*
Enumeration<String> emnu = Collections.enumeration(list);
while (emnu.hasMoreElements()) {
System.out.println(emnu.nextElement());
//xl
//xxl
//xxxl
//xxxxl
}
*/
//反转元素顺序
//Collections.reverse(list);
//排序
//Collections.sort(list);
//Collections.sort(list,Comparable);//后面可以传入比较器进行比较
//返回一个线程安全的list
//Collections.synchronizedList(list);
//Collections.synchronizedMap(Map);
}
}