【Map】Map集合有序与无序测试案例(121)

简单介绍常用的三种Map:不足之处,欢迎指正!

HashMap:put数据是无序的;
TreeMap:key值按一定的顺序排序;数字做key,put数据是有序,非数字字符串做key,put数据无序;
LinkedHashMap:LinkedHashMap是有序的,且默认为插入顺序;

测试案例

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class day12 {
	public static void main(String[] args) {
		
		List<String> list1 = Arrays.asList("黄","河","之","水","天","上","来","奔","流","到","海","不","复","回");
		List<Integer> list2 = Arrays.asList(1  , 2  , 3 , 4  ,  5 , 6  ,7  ,  8 , 9  ,10  , 11 ,12 , 13 , 14);

		Map<String,Object> hashMap = new HashMap<>();
		Map<String,Object> treeMap = new TreeMap<>();
		Map<String,Object> linkedHashMap = new LinkedHashMap<>();
		Map<Integer,Object> treeMap2 = new TreeMap<>();
		
		for (int i = 0; i < list1.size(); i++) {
			hashMap.put(list1.get(i), list2.get(i));
			treeMap.put(list1.get(i), list2.get(i));
			linkedHashMap.put(list1.get(i), list2.get(i));
			treeMap2.put(list2.get(i), list1.get(i));
		}
		
		List<String> list5 = new ArrayList<>();
		List<String> list6 = new ArrayList<>();
		
		Iterator<Map.Entry<String, Object>> it = hashMap.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, Object> entry = it.next();
            String key = entry.getKey();
            String value = String.valueOf(entry.getValue());
            list5.add(key);
            list6.add(value);
        }
        System.out.println("hashMap:"+"无序案例");
        System.out.println("hashMap:"+hashMap);
        System.out.println("list5ap:"+list5);
        System.out.println("list6ap:"+list6);
        System.out.println("--------------------------------------------------");
        
        List<String> list7 = new ArrayList<>();
		List<String> list8 = new ArrayList<>();
		
		Iterator<Map.Entry<String, Object>> it2 = treeMap.entrySet().iterator();
        while(it2.hasNext()){
            Map.Entry<String, Object> entry = it2.next();
            String key = entry.getKey();
            String value = String.valueOf(entry.getValue());
            list7.add(key);
            list8.add(value);
        }
        System.out.println("treeMap:"+"无序案例");
        System.out.println("treeMap:"+treeMap);
        System.out.println("list7ap:"+list7);
        System.out.println("list8ap:"+list8);
        System.out.println("--------------------------------------------------");
        
        List<String> list9 = new ArrayList<>();
		List<String> list10 = new ArrayList<>();
		
		Iterator<Map.Entry<String, Object>> it3 = linkedHashMap.entrySet().iterator();
        while(it3.hasNext()){
            Map.Entry<String, Object> entry = it3.next();
            String key = entry.getKey();
            String value = String.valueOf(entry.getValue());
            list9.add(key);
            list10.add(value);
        }
        System.out.println("linkedHashMap:"+"有序案例");
        System.out.println("linkedHashMap:"+linkedHashMap);
        System.out.println("list9dHashMap:"+list9);
        System.out.println("list10dHashMap:"+list10);
        System.out.println("--------------------------------------------------");
        
        
        List<String> list11 = new ArrayList<>();
		List<String> list12 = new ArrayList<>();
		
		Iterator<Map.Entry<Integer,Object>> it4 = treeMap2.entrySet().iterator();
        while(it4.hasNext()){
            Map.Entry<Integer,Object> entry = it4.next();
            Integer key = entry.getKey();
            String value = String.valueOf(entry.getValue());
            list11.add(key.toString());
            list12.add(value);
        }
        System.out.println("treeMap2:"+"有序案例");
        System.out.println("treeMap2:"+treeMap2);
        System.out.println("list11ap:"+list11);
        System.out.println("list12ap:"+list12);
        
	}
}

测试输出

hashMap:无序案例
hashMap:{=9,=1,=7,=5,=6,=3,=12,=13,=10,=2,=4,=8,=11,=14}
list5ap:[,,,,,,,,,,,,,]
list6ap:[9, 1, 7, 5, 6, 3, 12, 13, 10, 2, 4, 8, 11, 14]
--------------------------------------------------
treeMap:无序案例
treeMap:{=6,=12,=3,=10,=14,=13,=5,=8,=7,=4,=2,=9,=11,=1}
list7ap:[,,,,,,,,,,,,,]
list8ap:[6, 12, 3, 10, 14, 13, 5, 8, 7, 4, 2, 9, 11, 1]
--------------------------------------------------
linkedHashMap:有序案例
linkedHashMap:{=1,=2,=3,=4,=5,=6,=7,=8,=9,=10,=11,=12,=13,=14}
list9dHashMap:[,,,,,,,,,,,,,]
list10dHashMap:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
--------------------------------------------------
treeMap2:有序案例
treeMap2:{1=, 2=, 3=, 4=, 5=, 6=, 7=, 8=, 9=, 10=, 11=, 12=, 13=, 14=}
list11ap:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
list12ap:[,,,,,,,,,,,,,]

你可能感兴趣的:(java,ListMap,windows)