Stream的去重

public static void main(String[] args) {
        List> list = new ArrayList<>();
        Map map = new HashMap<>();
        Map map1 = new HashMap<>();
        Map map2 = new HashMap<>();
        map.put("name", "张三");
        map.put("phone", "17600000001");

        map1.put("name", "李四");
        map1.put("phone", "17600000001");

        map2.put("name", "王五");
        map2.put("phone", "17600000002");

        list.add(map);
        list.add(map1);
        list.add(map2);

        List> result = new ArrayList<>();
        for (Map mapTemp : list) {
            boolean b = result.stream().anyMatch(p -> p.get("phone").compareTo(mapTemp.get("phone")) == 0);
            if (!b) {
                result.add(mapTemp);
            }
        }
        System.out.println(result.toString());
    }

打印结果:[{phone=17600000001, name=张三}, {phone=17600000002, name=王五}]

你可能感兴趣的:(【Java】)