Java 集合轉換(數組、List、Set、Map相互轉換)

轉載自https://blog.csdn.net/top_code/article/details/10552827

package com.example.test;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
public class ConvertorTest {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		testList2Array();
        testArray2List();
        testSet2List();
		testList2Set();
		testSet2Array();
		testArray2Set();
		testMap2Set();
		testMap2List();
	}
 
	private static void testMap2List() {
		
		Map<String, String> map = new HashMap<String, String>();  
        map.put("A", "ABC");  
        map.put("K", "KK");  
        map.put("L", "LV");  
        
		// 將Map Key 轉化爲List    
        List<String> mapKeyList = new ArrayList<String>(map.keySet());  
        System.out.println("mapKeyList:"+mapKeyList);
        
        // 將Map Key 轉化爲List    
        List<String> mapValuesList = new ArrayList<String>(map.values());  
        System.out.println("mapValuesList:"+mapValuesList);
        
	}
 
	private static void testMap2Set() {
		
        Map<String, String> map = new HashMap<String, String>();  
        map.put("A", "ABC");  
        map.put("K", "KK");  
        map.put("L", "LV");  
        
        // 將Map 的鍵轉化爲Set    
        Set<String> mapKeySet = map.keySet();  
        System.out.println("mapKeySet:"+mapKeySet);
        
        // 將Map 的值轉化爲Set    
        Set<String> mapValuesSet = new HashSet<String>(map.values());  
        System.out.println("mapValuesSet:"+mapValuesSet);
	}
 
	private static void testArray2Set() {
		
        String[] arr = {"AA","BB","DD","CC","BB"};  
        
        //數組-->Set  
        Set<String> set = new HashSet<String>(Arrays.asList(arr));  
        System.out.println(set);  
	}
 
	private static void testSet2Array() {
		Set<String> set = new HashSet<String>();
		set.add("AA");
		set.add("BB");
		set.add("CC");
		
		String[] arr = new String[set.size()];  
		//Set-->數組  
		set.toArray(arr); 
        System.out.println(Arrays.toString(arr));  
	}
 
	private static void testList2Set() {
		  
		List<String> list = new ArrayList<String>();
		list.add("ABC");
		list.add("EFG");
		list.add("LMN");
		list.add("LMN");
		
		//List-->Set
        Set<String> listSet = new HashSet<String>(list);
        System.out.println(listSet);
	}
 
	private static void testSet2List() {
		 
		Set<String> set = new HashSet<String>();
		set.add("AA");
		set.add("BB");
		set.add("CC");
		
		//Set --> List
        List<String> setList = new ArrayList<String>(set);
        System.out.println(setList);  
	}
 
	private static void testList2Array() {
		//List-->數組  
        List<String> list = new ArrayList<String>();  
        list.add("AA");  
        list.add("BB");  
        list.add("CC");  
        Object[] objects = list.toArray();//返回Object數組  
        System.out.println("objects:"+Arrays.toString(objects));  
        
        String[] arr = new String[list.size()];  
        list.toArray(arr);//將轉化後的數組放入已經創建好的對象中  
        System.out.println("strings1:"+Arrays.toString(arr));  
	}
	
	private static void testArray2List() {
		//數組-->List  
        String[] ss = {"JJ","KK"};  
        List<String> list1 = Arrays.asList(ss);  
        List<String> list2 = Arrays.asList("AAA","BBB");  
        System.out.println(list1);  
        System.out.println(list2);  
	}
 
}

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