Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java stream distinct by object atribute

public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    Set<Object> seen = ConcurrentHashMap.newKeySet();
    return t -> seen.add(keyExtractor.apply(t));
}

persons.stream().filter(distinctByKey(Person::getName))
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #java #stream #distinct #object #atribute
ADD COMMENT
Topic
Name
8+4 =