集合工具類Collections代碼詳解

簡述

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));
	}

}

同步控制

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}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章