In Java 8 how can I filter a collection using the Stream
API by checking the distinctness of a property of each object?
For example I have a list of Person
object and I want to remove people with the same name,
persons.stream().distinct();
Will use the default equality check for a Person
object, so I need something like,
persons.stream().distinct(p -> p.getName());
Unfortunately the distinct()
method has no such overload. Without modifying the equality check inside the Person
class is it possible to do this succinctly?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…