Introduction

Introduced in Java 8, the Stream API is commonly used for filtering, mapping and iterating over elements. When working with streams, one of the common tasks is finding duplicate elements.

In this tutorial, we’ll be covering several ways to find duplicate elements in a Java Stream.

Collectors.toSet()

The easiest way to find duplicate elements is by adding the elements into a SetSets can’t contain duplicate values, and the Set.add() method returns a boolean value which is the result of the operation. If an element isn’t added, false is returned, and vice versa.

Let’s make a Stream of Strings with some duplicate values. These values are checked via the equals() method, so make sure to have an adequately implemented one for custom classes:

Stream<String> stream = Stream.of("john", "doe", "doe", "tom", "john");

Now, let’s make a Set to store the filtered items. We’ll use the filter() method to filter out duplicate values and return them:

Set<String> items = new HashSet<>();

stream.filter(n -> !items.add(n))
        .collect(Collectors.toSet())
        .forEach(System.out::println);

Here, we try to add() each element to the Set. If it’s not added, due to it being duplicate, we collect that value and print it out:

john
doe

#java

Java: Finding Duplicate Elements in a Stream
1.15 GEEK