Collections工具類提供了大量針對Collection/Map的操做,整體可分爲四類,都爲靜態(static)方法:java
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孫六]多線程
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]ide
Collections工具類中提供了多個synchronizedXxx方法,該方法返回指定集合對象對應的同步對象,從而解決多線程併發訪問集合時線程的安全問題。HashSet、ArrayList、HashMap都是線程不安全的,若是須要考慮同步,則使用這些方法。這些方法主要有:synchronizedSet、synchronizedSortedSet、synchronizedList、synchronizedMap、synchronizedSortedMap。工具
特別須要指出的是,在使用迭代方法遍歷集合時須要手工同步返回的集合。測試
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()); }
Collections有三類方法可返回一個不可變集合:spa
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. 其它線程
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); }
輸出code
true false
[我也好, 你們好, 你好]
package com.clzhang.sample.collections; 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); } }