【Map】Map集合有序与无序测试案例:HashMap,TreeMap,LinkedHashMap(121)

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

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

测试案例

java 复制代码
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);
        
	}
}

测试输出

java 复制代码
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:[黄, 河, 之, 水, 天, 上, 来, 奔, 流, 到, 海, 不, 复, 回]
相关推荐
这儿有一堆花14 小时前
用原生脚本编写无害恶作剧
windows
因我你好久不见14 小时前
Windows部署springboot jar支持开机自启动
windows·spring boot·jar
夜流冰15 小时前
Excel - MS Support for Excel: 2 Collaborate
数据库·windows·excel
林瞅瞅15 小时前
PowerShell 启动卡顿?内存飙升?原来是 800MB 的历史记录在作祟!
windows
Shepherd061916 小时前
【Windows Server 实战】WAC 反向代理配置
windows
云小逸16 小时前
【windows系统编程】第一章 Windows 系统核心架构与基础概念
windows·架构
怣疯knight18 小时前
Docker Desktop 4.55.0版本安装成功教程
windows·docker
liulilittle19 小时前
VEthernet 框架实现 tun2socks 的技术原理
网络·windows·c#·信息与通信·通信
独钓寒江雨20 小时前
win11在安全模式下删除360tray.exe
windows·电脑
PieroPc20 小时前
Windows 远程到 PVE 9.X Mac os (像window远程桌面)
windows·mac·远程桌面