I have searched for a solution for how to remove duplicates from a list with Stream API
found only this question How to remove duplicates from list of objects by id
I have a list of Person i need to filter by the person name, tried with the below snippet but it doesn't filter by the name
private static Map<Person, Integer> getUniqueByBiggerId(Collection<Person> persons) {
return persons.stream().collect(Collectors.toMap(
persons ->
persons,
Person::getId,
(id1, id2) -> {
if (id2 > id1)
return id2;
else
return id1;
}
));
}
public static void main(String args[]){
//.........
List<Person> Persons
= Arrays.asList(Person1, Person2,Person2,Person1,Person2, Person1);
getUniqueByBiggerId(Persons);
}