相对高效的遍历Map的方法,其他方法效率相对较低

  @Test
    public void test02() {
        Map<String,Integer> map = new HashMap<>();
        map.put("a", 21);
        map.put("b", 22);
        map.put("c", 23);
        Set<Entry<String,Integer>> entrySet = map.entrySet();
        for (Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + "---->" + entry.getValue());
        }
    }

如上代码,新建一个Map,然后放入K和V,最后遍历,使用JUnit5测试该方法,控制台输出效果如下:

原文地址:https://www.cnblogs.com/elnimo/p/10041506.html