Stream的去重
Posted Summer-Zheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Stream的去重相关的知识,希望对你有一定的参考价值。
public static void main(String[] args)
List<Map<String, String>> list = new ArrayList<>();
Map<String, String> map = new HashMap<>();
Map<String, String> map1 = new HashMap<>();
Map<String, String> 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<Map<String, String>> result = new ArrayList<>();
for (Map<String, String> 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=王五]
以上是关于Stream的去重的主要内容,如果未能解决你的问题,请参考以下文章