java8 foreach + 拉姆达表达式遍历Map

 @Test
    void testJava8ForeachMap() {
        Map items = new HashMap<>();
        items.put("A", 10);
        items.put("B", 20);
        items.put("C", 30);
        items.put("D", 40);
        items.put("E", 50);
        items.put("F", 60);

        //普通方式遍历Map
        for(Map.Entry entry:items.entrySet()){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }

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

        //java8 foreach + 拉姆达表达式遍历Map
        items.forEach((k, v) -> {
            System.out.println(k+":"+v);
        });

    }

结果:
java8 foreach + 拉姆达表达式遍历Map_第1张图片

你可能感兴趣的:(java8)