集合常用方法Collections
Collections是集合类的工具类,与数组的工具类Arrays类似
常用方法
addAll(Col lection c, T… elements);
Collections.addAll(list,a,b,c,d,e);
binarySearch(List> list, T key)
Collections.sort(list)
Collections.BinarySearch(list, key)
sort(List l ist)
Collections.sort(list)
sort(List l ist, Comparator c)
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
return o2-o1
}
});
System.out.println(Collections.binarySearch(list,data));
swap(List list, int i, int j)
Collections.swap(list,i,j);
copy(List dest, List src) ; 注意 dest size需大于等于src.size
Collections.copy(list1,list);
System.out.println(list1);
emptyList() 返回为空的集合,不能添加数据
List list = Collections.emptyList();
fill(List l ist, T obj)
Collections.fill(list,data);
System.out.println(list);
max(Collection coll)
System.out.println(Collections.max(list));
min(Col lection col l)
System.out.println(Collections.min(list));
replaceAll(List l ist, T oldVal, T newVal)
Collections.replaceAll(list,oldvalue,newvalue);
System.out.println(list);
reverse(List l ist)
Collections.reverse(list);
System.out.println(list);