How to Find Duplicate Elements in Java 8 Using Streams

Learn how to find duplicate elements in a List using Java 8 Stream , filter(), HashSet, Collections.frequency(), and Collectors.toSet() with practical examples.

Code Explanation (Step-by-Step) :

Using filter() and HashSet :
• Create a HashSet object of Integer type.
• Convert the array values into a List using Arrays.asList() .
• Get the stream from the List using listOne.stream().
• Inside the filter() method, use !hs.add(r) to check duplicate elements.
• Collect the duplicate elements using Collectors.toSet().
• Print the duplicate elements using forEach().

Using Collections.frequency() :
• Convert the array values into a List using Arrays.asList().
• Get the stream from the List using listTwo.stream().
• Inside the filter() method, use Collections.frequency(listTwo,e)>1.
• If frequency is greater than 1,the elements is considered duplicate.
• Collect the duplicate elements using Collectors.toSet().
• Print the duplicate elements using forEach().

				
					import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class FindDuplicateElements {

    public static void main(String[] args) {

        // Using filter() and HashSet
        List<Integer> listOne = Arrays.asList(18, 90, 22, 80, 98, 23, 87, 20, 80, 18, 22, 65, 21, 80);

        Set<Integer> hashSet = new HashSet<Integer>();

        System.out.println("Duplicate elements using filter() and HashSet:");

        listOne.stream()
                .filter(number -> !hashSet.add(number))
                .collect(Collectors.toSet())
                .forEach(System.out::println);


        // Using Collections.frequency() and filter()
        List<Integer> listTwo = Arrays.asList(18, 90, 22, 80, 98, 23, 87, 20, 80, 18, 22, 65, 21);

        System.out.println("Duplicate elements using Collections.frequency():");

        listTwo.stream()
                .filter(number -> Collections.frequency(listTwo, number) > 1)
                .collect(Collectors.toSet())
                .forEach(System.out::println);
    }
}
				
			

Output :
Duplicate elements using filter() and HashSet:
80
18
22
Duplicate elements using Collections.frequency():
80
18
22

Java 8 Streams to Find Duplicate Elements

				
					import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FindDuplicateElements {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(18, 90, 22, 80, 98, 23, 87, 20, 80, 18, 22, 65, 21);

        List<Integer> duplicateElements = numbers.stream()
                .collect(Collectors.groupingBy(
                        number -> number,
                        LinkedHashMap::new,
                        Collectors.counting()
                ))
                .entrySet()
                .stream()
                .filter(entry -> entry.getValue() > 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());

        System.out.println("Duplicate elements are: " + duplicateElements);
    }
}
				
			

Output :
Duplicate elements are: [18, 22, 80]

FAQ
How do you find duplicate elements in Java 8 Streams?
You can find duplicate elements in Java 8 Streams by using the filter() method with a HashSet. If the HashSet.add() method returns false, it means the element is already present and is duplicate.
Can we find duplicates using Collections.frequency()?
Yes, we can use Collections.frequency() to find duplicate elements. If the frequency of an element is greater than 1, then that element is duplicate.
Which method is better to find duplicates in Java 8?
The HashSet method is better for performance because it checks duplicate elements in a single stream operation. The Collections.frequency() method is easier to understand but slower for large lists.
Does Collectors.toSet() maintain order?
No, Collectors.toSet() does not guarantee insertion order. If you want to maintain order, you can use Collectors.toCollection(LinkedHashSet::new).