Collections工具類提供了大量針對Collection/Map的操作,總體可分為四類,都為靜態(static)方法:
1. 排序操作(主要針對List接口相關)
- ? reverse(List list):反轉指定List集合中元素的順序
- ? shuffle(List list):對List中的元素進行隨機排序(洗牌)
- ? sort(List list):對List里的元素根據自然升序排序
- ? sort(List list, Comparator c):自定義比較器進行排序
- ? swap(List list, int i, int j):將指定List集合中i處元素和j出元素進行交換
- ? rotate(List list, int distance):將所有元素向右移位指定長度,如果distance等于size那么結果不變
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public void testSort() { System.out.println( "原始順序:" + list); Collections.reverse(list); System.out.println( "reverse后順序:" + list); Collections.shuffle(list); System.out.println( "shuffle后順序:" + list); Collections.swap(list, 1 , 3 ); System.out.println( "swap后順序:" + list); Collections.sort(list); System.out.println( "sort后順序:" + list); Collections.rotate(list, 1 ); System.out.println( "rotate后順序:" + list); } |
輸出
原始順序:[b張三, d孫六, a李四, e錢七, c趙五]
reverse后順序:[c趙五, e錢七, a李四, d孫六, b張三]
shuffle后順序:[b張三, c趙五, d孫六, e錢七, a李四]
swap后順序:[b張三, e錢七, d孫六, c趙五, a李四]
sort后順序:[a李四, b張三, c趙五, d孫六, e錢七]
rotate后順序:[e錢七, a李四, b張三, c趙五, d孫六]
2. 查找和替換(主要針對Collection接口相關)
- ? binarySearch(List list, Object key):使用二分搜索法,以獲得指定對象在List中的索引,前提是集合已經排序
- ? max(Collection coll):返回最大元素
- ? max(Collection coll, Comparator comp):根據自定義比較器,返回最大元素
- ? min(Collection coll):返回最小元素
- ? min(Collection coll, Comparator comp):根據自定義比較器,返回最小元素
- ? fill(List list, Object obj):使用指定對象填充
- ? frequency(Collection Object o):返回指定集合中指定對象出現的次數
- ? replaceAll(List list, Object old, Object new):替換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public void testSearch() { System.out.println( "給定的list:" + list); System.out.println( "max:" + Collections.max(list)); System.out.println( "min:" + Collections.min(list)); System.out.println( "frequency:" + Collections.frequency(list, "a李四" )); Collections.replaceAll(list, "a李四" , "aa李四" ); System.out.println( "replaceAll之后:" + list); // 如果binarySearch的對象沒有排序的話,搜索結果是不確定的 System.out.println( "binarySearch在sort之前:" + Collections.binarySearch(list, "c趙五" )); Collections.sort(list); // sort之后,結果出來了 System.out.println( "binarySearch在sort之后:" + Collections.binarySearch(list, "c趙五" )); Collections.fill(list, "A" ); System.out.println( "fill:" + list); } |
輸出
給定的list:[b張三, d孫六, a李四, e錢七, c趙五]
max:e錢七
min:a李四
frequency:1
replaceAll之后:[b張三, d孫六, aa李四, e錢七, c趙五]
binarySearch在sort之前:-4
binarySearch在sort之后:2
fill:[A, A, A, A, A]
3. 同步控制
Collections工具類中提供了多個synchronizedXxx方法,該方法返回指定集合對象對應的同步對象,從而解決多線程并發訪問集合時線程的安全問題。HashSet、ArrayList、HashMap都是線程不安全的,如果需要考慮同步,則使用這些方法。這些方法主要有:synchronizedSet、synchronizedSortedSet、synchronizedList、synchronizedMap、synchronizedSortedMap。
特別需要指出的是,在使用迭代方法遍歷集合時需要手工同步返回的集合。
1
2
3
4
5
6
7
8
9
|
Map m = Collections.synchronizedMap( new HashMap()); ... Set s = m.keySet(); // Needn't be in synchronized block ... synchronized (m) { // Synchronizing on m, not s! Iterator i = s.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } |
4. 設置不可變集合
Collections有三類方法可返回一個不可變集合:
1. emptyXxx():返回一個空的不可變的集合對象
2. singletonXxx():返回一個只包含指定對象的,不可變的集合對象。
3. unmodifiableXxx():返回指定集合對象的不可變視圖
1
2
3
4
5
6
7
|
public void testUnmodifiable() { System.out.println( "給定的list:" + list); List<String> unmodList = Collections.unmodifiableList(list); unmodList.add( "再加個試試!" ); // 拋出:java.lang.UnsupportedOperationException // 這一行不會執行了 System.out.println( "新的unmodList:" + unmodList); } |
5. 其它
1. disjoint(Collection<?> c1, Collection<?> c2) - 如果兩個指定 collection 中沒有相同的元素,則返回 true。
2. addAll(Collection<? super T> c, T... a) - 一種方便的方式,將所有指定元素添加到指定 collection 中。示范:
Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
3. Comparator<T> reverseOrder(Comparator<T> cmp) - 返回一個比較器,它強行反轉指定比較器的順序。如果指定比較器為 null,則此方法等同于 reverseOrder()(換句話說,它返回一個比較器,該比較器將強行反轉實現 Comparable 接口那些對象 collection 上的自然順序)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public void testOther() { List<String> list1 = new ArrayList<String>(); List<String> list2 = new ArrayList<String>(); // addAll增加變長參數 Collections.addAll(list1, "大家好" , "你好" , "我也好" ); Collections.addAll(list2, "大家好" , "a李四" , "我也好" ); // disjoint檢查兩個Collection是否的交集 boolean b1 = Collections.disjoint(list, list1); boolean b2 = Collections.disjoint(list, list2); System.out.println(b1 + "\t" + b2); // 利用reverseOrder倒序 Collections.sort(list1, Collections.reverseOrder()); System.out.println(list1); } |
輸出
true false
[我也好, 大家好, 你好]
6. 完整代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package com.bjpowernode.test; import java.util.*; import org.junit.Before; import org.junit.Test; public class CollectionsTest { private List<String> list = new ArrayList<String>(); @Before public void init() { // 準備測試數據 list.add( "b張三" ); list.add( "d孫六" ); list.add( "a李四" ); list.add( "e錢七" ); list.add( "c趙五" ); } @Test public void testUnmodifiable() { System.out.println( "給定的list:" + list); List<String> unmodList = Collections.unmodifiableList(list); unmodList.add( "再加個試試!" ); // 拋出:java.lang.UnsupportedOperationException // 這一行不會執行了 System.out.println( "新的unmodList:" + unmodList); } @Test public void testSort() { System.out.println( "原始順序:" + list); Collections.reverse(list); System.out.println( "reverse后順序:" + list); Collections.shuffle(list); System.out.println( "shuffle后順序:" + list); Collections.swap(list, 1 , 3 ); System.out.println( "swap后順序:" + list); Collections.sort(list); System.out.println( "sort后順序:" + list); Collections.rotate(list, 1 ); System.out.println( "rotate后順序:" + list); } @Test public void testSearch() { System.out.println( "給定的list:" + list); System.out.println( "max:" + Collections.max(list)); System.out.println( "min:" + Collections.min(list)); System.out.println( "frequency:" + Collections.frequency(list, "a李四" )); Collections.replaceAll(list, "a李四" , "aa李四" ); System.out.println( "replaceAll之后:" + list); // 如果binarySearch的對象沒有排序的話,搜索結果是不確定的 System.out.println( "binarySearch在sort之前:" + Collections.binarySearch(list, "c趙五" )); Collections.sort(list); // sort之后,結果出來了 System.out.println( "binarySearch在sort之后:" + Collections.binarySearch(list, "c趙五" )); Collections.fill(list, "A" ); System.out.println( "fill:" + list); } @Test public void testOther() { List<String> list1 = new ArrayList<String>(); List<String> list2 = new ArrayList<String>(); // addAll增加變長參數 Collections.addAll(list1, "大家好" , "你好" , "我也好" ); Collections.addAll(list2, "大家好" , "a李四" , "我也好" ); // disjoint檢查兩個Collection是否的交集 boolean b1 = Collections.disjoint(list, list1); boolean b2 = Collections.disjoint(list, list2); System.out.println(b1 + "\t" + b2); // 利用reverseOrder倒序 Collections.sort(list1, Collections.reverseOrder()); System.out.println(list1); } } |
以上所述是小編給大家介紹的Collections工具類_動力節點Java學院整理,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!