Collection : 接口
Collections : 集合的工具类 Arrays (数组的工具类) 只能操作list集合
说出Collection和Collections 的区别?
Collection 是一个单列集合的根接口 ,Collections是操作集合的工具类。
Collections 中常用方法:
1, 对list进行二分查找:
前提该集合一定要有序。
int binarySearch(list,key);
必须根据元素自然顺序对列表进行升级排序
要求list 集合中的元素都是Comparable 的子类。
int binarySearch(list,key,Comparator);
2,对list集合进行排序。
sort(list);
对list进行排序,其实使用的事list容器中的对象的compareTo方法
sort(list,comaprator);
按照指定比较器进行排序
3,对集合取最大值或者最小值。
max(Collection)
max(Collection,comparator)
min(Collection)
min(Collection,comparator)
4,对list集合进行反转。
reverse(list);
5,对比较方式进行强行逆转。
Comparator reverseOrder();
Comparator reverseOrder(Comparator);
6,对list集合中的元素进行位置的置换。
swap(list,x,y);
7,对list集合进行元素的替换。如果被替换的元素不存在,那么原集合不变。
replaceAll(list,old,new);
8,可以将不同步的集合变成同步的集合。
Set synchronizedSet(Set<T> s)
Map synchronizedMap(Map<K,V> m)
List synchronizedList(List<T> list)
9. 如果想要将集合变数组:
可以使用Collection 中的toArray 方法。注意:是Collection不是Collections工具类
传入指定的类型数组即可,该数组的长度最好为集合的size。