本文包含以下打鉤知識點:
- [x] Collections類是什么?Collection 和 Collections的區別?
- [ ] 為什么 Collection 不從 Cloneable 和 Serializable 接口繼承
- [ ] 說出幾點 Java 中使用 Collections 的最佳實踐?
Collection和Collections的區別
Collections
java.util.Collections,是不屬于java的集合框架的,它是集合類的一個工具類/幫助類。此類不能被實例化, 服務于java的Collection框架。
它包含有關集合操作的靜態多態方法,實現對各種集合的搜索、排序、線程安全等操作。如:
常用的有:
(1)排序 sort(Collection)
如Collections.sort(List<T> list),Collections.sort(List<T> list, Comparator<? super T> c)。
使用sort方法可以根據元素的自然順序對指定列表按升序進行排序。列表中的所有元素都必須實現Comparable接口, 而且必須是使用指定比較器可相互比較的。
@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.sort(c);
}
簡單示例:
public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.sort(list);
System.out.println(list);
}
}
運行結果:
[five, four, one, three, two] //(按字母排序)
(2)混排 shuffle(Collection)
如Collections.shuffle(List<?> list)
基于隨機源的輸入隨機排序該List,這個算法對實現一個碰運氣的游戲非常有用,在生成測試案例時也十分有用。
簡單示例:
public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.shuffle(list);
System.out.println(list);
}
}
運行結果:
[three, five, four, one, two]
(3)反轉 reverse(Collection)
如Collections.reverse(List<?> list)
使用reverse()反轉集合中元素的順序
簡單示例:
public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.reverse(list);
System.out.println(list);
}
}
運行結果:
[five, four, three, two, one]
(4)替換所有元素 fill(List list,Object o)
使用指定元素替換集合中的所有元素。
簡單示例:
public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.fill(list, "zero");
System.out.println(list);
}
}
運行結果:
[zero, zero, zero, zero, zero]
(5)拷貝 copy(List list1,List list2)
將集合list2中的元素全部復制到list1中,并且覆蓋相應索引的元素。目標list1至少與源list2一樣長。
簡單示例:
public class Test {
public static void main(String[] args) {
List list1 = Arrays.asList("one two three four five".split(" "));
List list2 = Arrays.asList("一 二 三 四 五".split(" "));
Collections.copy(list1, list2);
System.out.println(list1);
}
}
運行結果:
[一, 二, 三, 四, 五]
(6)rotate(List list,int m)
根據指定的距離m循環移動列表中的元素。集合中的元素向后移m個位置,在后面被遮蓋的元素循環到前面來。
簡單示例:
public class Test {
public static void main(String[] args) {
List list1 = Arrays.asList("one two three four five".split(" "));
Collections.rotate(list1, 2);
System.out.println(list1);
}
}
運行結果:
[four, five, one, two, three]
(7)最小(大)元素 min(),max()
根據指定比較器產生的順序,返回給定Collection的最小(大)元素。
min(Collection),min(Collection,Comparator)
max(Collection),max(Collection,Comparator)
簡單示例:
public class Test {
public static void main(String[] args) {
List list = new ArrayList();
list.add(10);
list.add(40);
list.add(20);
list.add(50);
System.out.println(Collections.min(list));
System.out.println(Collections.max(list));
}
}
運行結果:
10
50
(8)indexOfSublist(List list,List sublist)
查找sublist在list中首次出現位置的索引。返回指定源列表中第一次出現指定目標列表的起始位置。
簡單示例:
public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
List subList=Arrays.asList("three four five".split(" "));
System.out.println(Collections.indexOfSubList(list,subList));
}
}
運行結果:
2
(9)lastIndexOfSublist(List list,List sublist)
返回指定列表中最后一次出現指定目標列表的起始位置。
(10)swap(List list,int m,int n)
交換集合中指定元素索引m,n的位置。
public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.swap(list, 2, 3);
System.out.println(list);
}
}
運行結果:
[one, two, four, three, five]
Collection
Collection是最基本的集合接口,一個Collection代表一組Object,即Collection的元素。它的直接繼承接口有List,Set和Queue。
Collection接口,Set、List接口參考
Java中集合類框架及基本接口