关于java:精髓Java中遍历Map集合的五种方式

8次阅读

共计 2719 个字符,预计需要花费 7 分钟才能阅读完成。

作者:扬帆向海

https://zhangxy.blog.csdn.net…

在 java 中所有的 map 都实现了 Map 接口,因而所有的 Map 都能够用以下的形式去遍历。这篇文章次要给大家介绍了对于 Java 中遍历 Map 汇合的 5 种形式,文中通过示例代码介绍的十分具体,对大家的学习或者工作具备肯定的参考学习价值,须要的敌人们上面一起学习学习吧。

形式一 通过 Map.keySet 应用 iterator 遍历

@Test
public void testHashMap1() {Map<Integer, String> map = new HashMap<>();
    map.put(001, "Java");
    map.put(002, "数据库");
    map.put(003, "Vue");
    System.out.println(map);

    
    Iterator<Integer> iterator = map.keySet().iterator();
    while (iterator.hasNext()) {Integer key = iterator.next();
        String value = map.get(key);
        System.out.println("key =" + key + ", value =" + value);
    }
}

后果:

{1=Java, 2= 数据库, 3=Vue}
key = 1, value = Java
key = 2, value = 数据库
key = 3, value = Vue

形式二 通过 Map.entrySet 应用 iterator 遍历

@Test
public void testHashMap2() {Map<Integer, String> map = new HashMap<>();
    map.put(001, "Java");
    map.put(002, "数据库");
    map.put(003, "Vue");
    System.out.println(map);

    
    Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
    while (entries.hasNext()) {Map.Entry<Integer, String> entry = entries.next();
        System.out.println(entry);
    }
}

后果:

{1=Java, 2= 数据库, 3=Vue}
1=Java
2= 数据库
3=Vue

形式三 通过 Map.keySet 遍历

@Test
public void testHashMap3() {Map<Integer, String> map = new HashMap<>();
    map.put(001, "Java");
    map.put(002, "数据库");
    map.put(003, "Vue");
    System.out.println(map);

    
    for (Integer key : map.keySet()) {System.out.println("key =" + key + ", value =" + map.get(key));
    }
}

后果:

{1=Java, 2= 数据库, 3=Vue}
key = 1, value = Java
key = 2, value = 数据库
key = 3, value = Vue

形式四 通过 For-Each 迭代 entries,应用 Map.entrySet 遍历

@Test
public void testHashMap4() {Map<Integer, String> map = new HashMap<>();
    map.put(001, "Java");
    map.put(002, "数据库");
    map.put(003, "Vue");
    System.out.println(map);

    
    for (Map.Entry<Integer, String> entry : map.entrySet()) {System.out.println("key =" + entry.getKey() + ", value =" + entry.getValue());
    }
}
{1=Java, 2= 数据库, 3=Vue}
key = 1, value = Java
key = 2, value = 数据库
key = 3, value = Vue

形式五 应用 lambda 表达式 forEach 遍历

@Test
public void testHashMap5() {Map<Integer, String> map = new HashMap<>();
    map.put(001, "Java");
    map.put(002, "数据库");
    map.put(003, "Vue");
    System.out.println(map);

    
    map.forEach((k, v) -> System.out.println("key =" + k + ", value =" + v));
}

forEach 源码

default void forEach(BiConsumer<? super K, ? super V> action) {Objects.requireNonNull(action);
        for (Map.Entry<K, V> entry : entrySet()) {
            K k;
            V v;
            try {k = entry.getKey();
                v = entry.getValue();} catch(IllegalStateException ise) {throw new ConcurrentModificationException(ise);
            }
            action.accept(k, v);
        }
    }

从源码能够看到,这种新个性就是在传统的迭代形式上加了一层壳,然而让代码变得更加简略。( 开发中举荐应用

总结

举荐应用 entrySet 遍历 Map 类汇合 KV(文章中的第四种形式),而不是 keySet 形式进行遍历。

keySet 其实是遍历了 2 次,第一次是转为 Iterator 对象,第二次是从 hashMap 中取出 key 所对应的 value 值。而 entrySet 只是遍历了一次,就把 key 和 value 都放到了 entry 中,效率更高。

values() 返回的是 V 值汇合,是一个 list 汇合对象;keySet() 返回的是 K 值汇合,是一个 Set 汇合对象;entrySet() 返回的是 K-V 值组合汇合。

如果是 JDK8,举荐应用 Map.forEach 办法 (文章中的第五种形式)。

因为程度无限,本博客不免有有余,恳请各位大佬不吝赐教!


举荐浏览

IDEA2020.2.3 破解,IDEA2020.2 激活破解,IDEA 激活码

太赞了,这个 Java 网站,什么我的项目都有!https://markerhub.com

这个 B 站的 UP 主,讲的 java 真不错!

B 站 15 万播放量,SpringBoot+Vue 前后端拆散残缺入门教程!

太赞了!最新版 Java 编程思维能够在线看了!

2021 年最新的常问企业面试题大全以及答案

正文完
 0