Collections工具類:用于操作Set、List、Map等集合
1、排序操作
提供了大量類方法用于對List集合元素進(jìn)行排序
- void reverse(List list):反轉(zhuǎn)集合順序
- void shuffle(List list):隨機(jī)排序
- void sort(List list):按升序排序
- void sort(List list, Comparator c):按Comparator產(chǎn)生的順序進(jìn)行排序(定制排序)
- void swap(List list, int i, int j):交換位置
- void rotate(List list, int distance):整體移動集合元素
2、查找、替換操作
- int binarySearch(List list, Object key):二分搜索法搜索指定的List集合獲得指定對象在List中的索引(前提:已排序)
- Object max(Collection coll):根據(jù)元素的自然排序返回集合中最大值
- Object max(Collection coll, Comparator comp):根據(jù)Comparator指定的順序返回集合中最大值
- Object min(Collection coll):根據(jù)元素的自然排序返回集合中最小值
- Object min(Collection coll, Comparator comp):根據(jù)Comparator指定的順序返回集合中最小值
- void fill(List list, Object obj):使用指定元素obj替換所有元素
- int frequency(Collection c, Object obj):返回集合中指定元素出現(xiàn)的次數(shù)
- int indexOfSubList(List Source, List target):返回子List對象在父List對象中第一次出現(xiàn)的索引
- int lastIndexOfSubList(List Source, List target):返回子List對象在父List對象中最后一次出現(xiàn)的索引
- boolean replaceAll(List list, Object oldVal, Object newVal):使用新值newVal替換List對象中的所有舊值oldVal
3、同步控制
提供了多個synchronizedXxx()方法,用于將指定集合包裝成線程同步的集合,解決線程不安全的問題
- 普通創(chuàng)建
Collection c = new ArrayList(); - 線程安全的創(chuàng)建
Collection c = Collections.synchronizedCollection(new ArrayList())
4、設(shè)置不可變集合
只能訪問(只讀),不可修改(寫入)