Java 8 如何在具有列表流的映射中查找重复值?
假设我有这个数组列表:Java 8 如何在具有列表流的映射中查找重复值?,java-8,Java 8,假设我有这个数组列表: List<Map<String,String>> fileDataList= new ArrayList<>(); fileDataList.stream().forEach(t->{ //find duplicate map values }); 现在,我想在流中找到匹配名称和年龄的重复映射值,而不删除它们。 我尝试使用HashSet,但我无法理解。霍尔格在评论中提出了一个我没有提到的观点。如果您的Map仅包含那些n
List<Map<String,String>> fileDataList= new ArrayList<>();
fileDataList.stream().forEach(t->{
//find duplicate map values
});
现在,我想在流中找到匹配名称和年龄的重复映射值,而不删除它们。
我尝试使用HashSet,但我无法理解。霍尔格在评论中提出了一个我没有提到的观点。如果您的
Map
仅包含那些name
和age
属性,您可以简单地执行以下操作:
fileDataList.stream()
.distinct()
.collect(Collectors.toList())
这就足够了。另一方面,如果您有更多的属性和
您可以使用以下方法仅通过其中的一部分来过滤:
如果
映射到入口集的列表,则可以将列表转换为入口集的列表
List<Map.Entry<String, String>> entries = fileDataList.stream()
.flatMap(e -> e.entrySet().stream())
.collect(toList());
List entries=fileDataList.stream()
.flatMap(e->e.entrySet().stream())
.collect(toList());
谢谢@Eugene。我想这对我的案子会有用的。
fileDataList.stream()
.filter(distinctByKey(x -> Arrays.asList(x.get("name"), x.get("age")))
.collect(Collectors.toList());
List<Map.Entry<String, String>> entries = fileDataList.stream()
.flatMap(e -> e.entrySet().stream())
.collect(toList());