簡述java
Collections:操做Set、List和Map等集合的工具類,主要包含對元素排序、查詢、修改、設置不可變和同步控制等方法
安全
排序操做 多線程
import java.util.ArrayList; import java.util.Collections; public class sortedTest { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add(0); list.add(19); list.add(23); list.add(-2); System.out.println("正常輸出:"+list); //反轉指定list集合中元素的順序 Collections.reverse(list); System.out.println("反轉輸出:"+list); //升序排序 Collections.sort(list); System.out.println("升序輸出:"+list); //隨機排序,洗牌 Collections.shuffle(list); System.out.println("隨機洗牌:"+list); //將i處元素和j處元素進行交換 Collections.swap(list, 0, 3); System.out.println("交換輸出:"+list); //n爲正數,後n個元素移到前面,n爲負數,前n個元素移到後面 Collections.rotate(list, 2); System.out.println("前移輸出:"+list); //比較器排序,降序 Collections.sort(list, (o1,o2)->{ return (Integer)o1>(Integer)o2?-1:(Integer)o1<(Integer)o2?1:0; }); System.out.println("降序輸出:"+list); } }
查找、替換併發
import java.util.ArrayList; import java.util.Collections; public class searchTest { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(0); numbers.add(2); numbers.add(23); numbers.add(-9); numbers.add(2); numbers.add(2); System.out.println(numbers); //按元素的天然排序 System.err.println("最大元素:" + Collections.max(numbers)); System.err.println("最小元素:" + Collections.min(numbers)); //集合中指定元素的出現次數 System.out.println("2在numbers中出現的次數:" + Collections.frequency(numbers, 2)); //使用12替換numbers中的全部2 Collections.replaceAll(numbers, 2, 12); System.out.println("使用12替換numbers中的全部2: "+numbers); //numbers中的元素處於有序狀態才能使用二分查找法 Collections.sort(numbers); System.out.println("排序: "+numbers); System.out.println("23在numbers中的索引: "+Collections.binarySearch(numbers, 23)); } }
同步控制ide
Collections類中提供了多個synchronizedXxx()方法,將指定集合包裝成線程同步的集合,從而解決多線程併發訪問集合時的線程安全問題 //線程安全的集合對象,直接將新建立的集合對象傳給Collections的synchronizedXxx()方法 Collection c = Collections.synchronizedCollection(new ArrayList()); List list = Collections.synchronizedList(new arrayList()); Set s = Collections.synchronizedSet(new HashSet()); Map m = Collections.synchronizedMap(new HashMap()); 與Vector相似,儘可能少用Hashtable,即便須要建立線程安全的Map實現類,可經過Collections工具類把HashMap變成線程安全的 如何用Collections工具類把HashMap變成線程安全的? Map m = Collections.synchronizedMap(new HashMap())
設置不可變集合
工具
import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /*設置不可變集合,只能訪問,不能修改 * Collections.emptyXxx():返回一個空的、不可變的集合,可爲List、Map、SortedMap、Set、SortedSet * singletonXxx():返回一個只包含指定對象的、不可變的集合,可爲List、Map * unmodifiableXxx():返回集合對象的不可變視圖,可爲List、Map、SortedMap、Set、SortedSet * 程序可直接調用Set、List、Map的of()方法便可建立包含N個元素的不可變集合 * */ public class unmodifiableCollections { @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) { // TODO Auto-generated method stub //返回一個空的、不可變的集合 List emptyList = Collections.EMPTY_LIST; //返回一個只包含指定對象的、不可變的集合 Set singletonSet = Collections.singleton("你好"); //普通Map集合 Map scores = new HashMap(); scores.put("語文", 87); scores.put("數學", 97); //返回集合對象的不可變視圖 Map unmodifiableMap = Collections.unmodifiableMap(scores); //建立包含三個元素的Set集合 Set set = Set.of("Python","Java","Qt"); //建立包含4個元素的List List list = List.of(1,2,3,4); //建立包含三個key-value對的Map集合 Map map = Map.of("語文",98,"數學",97,"英語",90); Map map1 = Map.ofEntries(Map.entry("語文", 98),Map.entry("數學", 97),Map.entry("英語", 97)); System.out.println(emptyList); //[] System.out.println(unmodifiableMap); //{數學=97, 語文=87} System.out.println(singletonSet); //[你好] System.out.println(list); //[1, 2, 3, 4] System.out.println(set); //[Python, Java, Qt] System.out.println(map); //{英語=90, 數學=97, 語文=98} System.out.println(map1); //{數學=97, 英語=97, 語文=98} } }