Java 8 Distinct by property

RichK

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?

Stuart Marks

Consider distinct to be a stateful filter. Here is a function that returns a predicate that maintains state about what it's seen previously, and that returns whether the given element was seen for the first time:

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

Then you can write:

persons.stream().filter(distinctByKey(Person::getName))

Note that if the stream is ordered and is run in parallel, this will preserve an arbitrary element from among the duplicates, instead of the first one, as distinct() does.

(This is essentially the same as my answer to this question: Java Lambda Stream Distinct() on arbitrary key?)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

java 8 stream collect max object and distinct by Property

From Dev

java 8 stream collect max object and distinct by Property

From Java

java 8 grouping by with distinct count

From Dev

java 8 grouping by with distinct count

From Dev

Java 8 Stream distinct is not working

From Dev

Distinct objects from property GS collections Java

From Java

Java 8 stream sort by maximum duplicates and then distinct

From Dev

Trying to understand distinct() for streams in Java8

From Dev

java 8 - stream, map and count distinct

From Dev

Java 8 stream sort by maximum duplicates and then distinct

From Dev

Java 8 distinct() does not invoke the equals method

From Dev

combination of .distinct() and .sum() in java8

From Dev

Sorting by property in Java 8 stream

From Dev

Using Group By in Java 8 with Property

From Dev

Get Array of Property, of Property (nested property) using stream Java 8

From Dev

Java8 Streams - Remove Duplicates With Stream Distinct

From Dev

How to get distinct list of object with concat in Java 8

From Dev

What is the best way to aggregate Streams into one DISTINCT with Java 8

From Dev

Sort by comparable Bean property in Java 8

From Dev

Getting the max of a property using Java 8

From Dev

Java 8 Comparator chaining with a reverse on single property

From Dev

Java 8 Stream Filer based on Object property

From Dev

Remove duplicates based on property and predicate in Java 8

From Dev

Java 8: Getting a property from a List of a List

From Dev

Java 8 sort on Class member's property

From Java

LINQ's Distinct() on a particular property

From Dev

RavenDB Select Distinct on a single property

From Dev

Select distinct values of property with grouping

From Dev

How to get the first value for each distinct keys using Java 8 streams?

Related Related

  1. 1

    java 8 stream collect max object and distinct by Property

  2. 2

    java 8 stream collect max object and distinct by Property

  3. 3

    java 8 grouping by with distinct count

  4. 4

    java 8 grouping by with distinct count

  5. 5

    Java 8 Stream distinct is not working

  6. 6

    Distinct objects from property GS collections Java

  7. 7

    Java 8 stream sort by maximum duplicates and then distinct

  8. 8

    Trying to understand distinct() for streams in Java8

  9. 9

    java 8 - stream, map and count distinct

  10. 10

    Java 8 stream sort by maximum duplicates and then distinct

  11. 11

    Java 8 distinct() does not invoke the equals method

  12. 12

    combination of .distinct() and .sum() in java8

  13. 13

    Sorting by property in Java 8 stream

  14. 14

    Using Group By in Java 8 with Property

  15. 15

    Get Array of Property, of Property (nested property) using stream Java 8

  16. 16

    Java8 Streams - Remove Duplicates With Stream Distinct

  17. 17

    How to get distinct list of object with concat in Java 8

  18. 18

    What is the best way to aggregate Streams into one DISTINCT with Java 8

  19. 19

    Sort by comparable Bean property in Java 8

  20. 20

    Getting the max of a property using Java 8

  21. 21

    Java 8 Comparator chaining with a reverse on single property

  22. 22

    Java 8 Stream Filer based on Object property

  23. 23

    Remove duplicates based on property and predicate in Java 8

  24. 24

    Java 8: Getting a property from a List of a List

  25. 25

    Java 8 sort on Class member's property

  26. 26

    LINQ's Distinct() on a particular property

  27. 27

    RavenDB Select Distinct on a single property

  28. 28

    Select distinct values of property with grouping

  29. 29

    How to get the first value for each distinct keys using Java 8 streams?

HotTag

Archive