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.
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:
Drop me your questions in comments.
Happy Learning !!
Reference:
Java Stream interface