Java遍历Map对象的四种方法(HashMap泛型)

添加map模拟数据

		Map<String,Object> map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put("syx" + i, "123" + i);
        }

1.使用entrySet遍历

  • 遍历map数据
	   for (Map.Entry<String, Object> m : map.entrySet()){
            System.out.print("key: " + m.getKey());
            System.out.print("value: " + m.getValue());
        }

2.使用keySet、values遍历

  • 遍历map数据中的key
	   for (String m : map.keySet()){
            System.out.print("key: " + m);
        }
  • 遍历map数据中的value
	   for (String m : map.values()){
            System.out.print("values: " + m);
        }

3.使用Iterator遍历

  • 遍历map数据
		Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Object> entry = iterator.next();
            System.out.print("key: " + entry.getKey());
            System.out.print("value: " + entry.getValue());
        }

4.使用Lambda遍历

  • 遍历map数据
 		map.forEach((key,value)->{
            System.out.print("key: " + key);
            System.out.print("value: " + value);
        });

5.总结

同时遍历10000条数据时的用时

随机执行5次

方法第一次(ms)第二次(ms)第三次(ms)第四次(ms)第五次(ms)
entrySet用时217192253179222
keySet用时88481094151
values用时5363413843
Iterator用时231110657262
lambda用时292438441306272
  • 同时获取key和value时: Iterator > entrySe > lambda
  • 只使用key或value时: keySet、values








全部代码如下

// 遍历map泛型
        Map<String,Object> map = new HashMap<>();
        for (int i = 0; i < 10000; i++) {
            map.put("syx" + i, "123" + i);
        }

        /**
        * entrySet
        * */
        //直接遍历出key、value
        System.out.println("---------------------1 entrySet Map<String,Object> ");
        long startTime = System.currentTimeMillis();
        for (Map.Entry<String, Object> m : map.entrySet()){
            System.out.print("key: " + m.getKey());
            System.out.print("value: " + m.getValue());
        }
        long endTime = System.currentTimeMillis();

        /**
         * keySet、values
         * */
        // 根据key遍历出value
        System.out.println("---------------------2 keySet Map<String,Object>---------------------------");
        long startTime1 = System.currentTimeMillis();
        for (String m : map.keySet()){
            System.out.print("key: " + m);
        }
        long endTime1 = System.currentTimeMillis();

        System.out.println("---------------------3 values Map<String,Object>---------------------------");
        // 遍历value
        long startTime2 = System.currentTimeMillis();
        for (Object m : map.values()){
            System.out.print("value: " + m);
        }
        long endTime2 = System.currentTimeMillis();

        /**
         * Iterator
         * */
        System.out.println("---------------------4 iterator Map<String,Object>---------------------------");
        long startTime3 = System.currentTimeMillis();
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Object> entry = iterator.next();
            System.out.print("key: " + entry.getKey());
            System.out.print("value: " + entry.getValue());
        }
        long endTime3 = System.currentTimeMillis();


        System.out.println("---------------------5 Lambda Map<String,Object>---------------------------");
        long startTime4 = System.currentTimeMillis();
        map.forEach((key,value)->{
            System.out.print("key: " + key);
            System.out.print("value: " + value);
        });
        long endTime4 = System.currentTimeMillis();


        System.out.println("----------------------------------------");


        System.out.println("entrySet用时:" + (endTime-startTime));
        System.out.println("keySet用时:" + (endTime1-startTime1));
        System.out.println("values用时:" + (endTime2-startTime2));
        System.out.println("Iterator用时:" + (endTime3-startTime3));
        System.out.println("lambda用时:" + (endTime4-startTime4));
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值