3

Say I have this array list:

 List<Map<String,String>> fileDataList= new ArrayList<>();
 fileDataList.stream().forEach(t->{
  //find duplicate map values
 });

The list may contain duplicate values like these:

[
    {age:12,name:"john"}, 
    {age:11,name:"Mary"},
    {age:12,name:"john"}
]

Now,I would like to find the duplicate map values which match both name and age inside the stream without removing them. I tried with HashSet, But I couldn't understand.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25

2 Answers2

3

Holger in comments brings a point I missed. If your Map contains only of those name and age properties, you could simply do:

fileDataList.stream()
            .distinct()
            .collect(Collectors.toList())

And this will be enough. If, on the other hand, you have more properties and what to filter by only some of them, you could use this utility:

fileDataList.stream()
            .filter(distinctByKey(x -> Arrays.asList(x.get("name"), x.get("age")))
            .collect(Collectors.toList());
Eugene
  • 117,005
  • 15
  • 201
  • 306
0

You can transfre your list if Map to list of EntrySet.

List<Map.Entry<String, String>> entries = fileDataList.stream()
                .flatMap(e -> e.entrySet().stream())
                .collect(toList());
dehasi
  • 2,644
  • 1
  • 19
  • 31