Java 8 check duplicates in list of objects

Few simple examples to find or count the duplicates in stream and remove the duplicates from stream in Java 8. We will use ArrayList to provide stream of elements including duplicates.

1. Stream.distinct[] to remove duplicates

The distinct[] method returns a stream consisting of the distinct elements of given stream. The element equality is checked according to elements equals[] method.

// ArrayList with duplicate elements ArrayList numbersList = new ArrayList[Arrays.asList[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]]; List listWithoutDuplicates = numbersList.stream[] .distinct[] .collect[Collectors.toList[]]; System.out.println[listWithoutDuplicates];

Program output:

[1, 2, 3, 4, 5, 6, 7, 8]

2. Collectors.toSet[] to remove duplicates

Another simple and very useful way is to store all the elements in a Set. Sets, by definition, store only distinct elements.

// ArrayList with duplicate elements ArrayList numbersList = new ArrayList[Arrays.asList[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]]; Set setWithoutDuplicates = numbersList.stream[] .collect[Collectors.toSet[]]; System.out.println[setWithoutDuplicates];

Program output:

[1, 2, 3, 4, 5, 6, 7, 8]

3. Collectors.toMap[] to count occurances

Sometimes, we are interested in finding out that which all elements are duplicates and how many times they appeared in the original list. We can use a Map to store this information.

We have to iterate over the list, put element as the map key, and all its occurrences in the map value field.

// ArrayList with duplicate elements ArrayList numbersList = new ArrayList[Arrays.asList[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]]; Map elementCountMap = numbersList.stream[] .collect[Collectors.toMap[Function.identity[], v -> 1L, Long::sum]]; System.out.println[elementCountMap];

Program output:

{1=2, 2=1, 3=3, 4=1, 5=1, 6=3, 7=1, 8=1}

Drop me your questions in comments.

Happy Learning !!

Reference:

Java Stream interface

Was this post helpful?

Let us know if you liked the post. Thats the only way we can improve.
Yes
No

Video liên quan

Chủ Đề