I want to remove duplicates from a list based on a property(mail) of the items in the list.
I did this :
acteurs = acteurs.stream().distinct().collect(Collectors.toList());
The equals method of Acteur
@Override
public boolean equals(Object o) {
if (this.getMail().equals(((Acteur)o).getMail())) {
return true;
}
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Acteur acteur = (Acteur) o;
if(acteur.id == null || id == null) {
return false;
}
return Objects.equals(id, acteur.id);
}
The equals method should be called during the instruction
acteurs = acteurs.stream().distinct().collect(Collectors.toList());
But it isn't.
Where am I wrong?
UPDATE :
My solution :
List<Acteur> dto3s =acteurs.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Acteur:: getMail))),
ArrayList::new));