Java stream flatMap List「Map「Long,String」」 to Map

public void test1(){
        Map m1 = new HashMap<>();
        m1.put(1L,"test1");

        Map m2 = new HashMap<>();
        m2.put(2L,"test2");

        Map m3 = new HashMap<>();
        m3.put(3L,"test3");

        Map m4 = new HashMap<>();
        m4.put(4L,"test4");

        Map m5 = new HashMap<>();
        m5.put(4L,"test444444");

        List> list = new ArrayList<>();
        list.add(m1);
        list.add(m2);
        list.add(m3);
        list.add(m4);
        list.add(m5);

        Map map = list.stream().map(Map::entrySet).flatMap(Set::stream).distinct().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(k1,k2)->k2));

        for (Map.Entry entry : map.entrySet()){
            System.out.println(entry.getKey() + "-->" + entry.getValue());
        }
    }

你可能感兴趣的:(基础编程,java,stream,lambda)