vavr-io / vavr

vʌvr (formerly called Javaslang) is a non-commercial, non-profit object-functional library that runs with Java 8+. It aims to reduce the lines of code and increase code quality.
https://vavr.io
Other
5.73k stars 637 forks source link

Add Traversable.distinct by key extractor #106

Closed danieldietrich closed 9 years ago

danieldietrich commented 9 years ago

See jOOQ/jOOL#82

danieldietrich commented 9 years ago

Unlike the implementation suggested here, it is not necessary to make the implementation concurrent because List and Stream aren't. That leads to

<U> Stream<T> distinct(Function<? super T, ? extends U> keyExtractor) {
    final Set<U> seen = new HashSet<>();
    return filter(t -> seen.add(keyExtractor.apply(t)));
}