Find Duplicate Elements in Java 8 Streams

├Źndice
  1. Introduction
  2. Method 1: Using Collectors.groupingBy()
  3. Method 2: Using Set.add()
  4. Conclusion

Introduction

If you are working with Java 8 streams and need to find duplicate elements, there are a few ways to do it. In this article, we will explore some of the methods you can use to find duplicates in Java 8 streams.

Method 1: Using Collectors.groupingBy()

One way to find duplicate elements in a Java 8 stream is to use the Collectors.groupingBy() method. This method groups the elements in the stream based on a given key, and returns a Map that contains the grouped elements.

To find duplicates, we can group the elements by themselves, and then filter out the elements that only appear once.

Here's an example:


List<String> list = Arrays.asList("apple", "orange", "banana", "apple", "grape", "banana");

Map<String, Long> result =
    list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Set<String> duplicates =
    result.entrySet().stream()
        .filter(entry -> entry.getValue() > 1)
        .map(Map.Entry::getKey)
        .collect(Collectors.toSet());

System.out.println(duplicates);

In this example, we create a list of strings that contains some duplicates. We then use the Collectors.groupingBy() method to group the elements by themselves, and count how many times each element appears. We then filter out the elements that only appear once, and collect the keys of the remaining elements into a Set. Finally, we print out the set of duplicates.

The output of this code will be:

[banana, apple]

This method works well for small streams, but can be inefficient for larger streams, as it requires creating a Map that contains all the elements in the stream.

Method 2: Using Set.add()

Another way to find duplicate elements in a Java 8 stream is to use a Set to keep track of the elements that have already been seen. We can use the Set.add() method to add each element to the set, and check if the add() method returns false, which indicates that the element is a duplicate.

Here's an example:


List<String> list = Arrays.asList("apple", "orange", "banana", "apple", "grape", "banana");

Set<String> seen = new HashSet<>();
Set<String> duplicates =
    list.stream()
        .filter(e -> !seen.add(e))
        .collect(Collectors.toSet());

System.out.println(duplicates);

In this example, we create a list of strings that contains some duplicates. We then create a HashSet to keep track of the elements that have already been seen. We use the filter() method to check if the add() method returns false for each element in the stream, which indicates that the element is a duplicate. We then collect the duplicates into a Set, and print out the result.

The output of this code will be:

[banana, apple]

This method is more efficient than the Collectors.groupingBy() method, as it only requires creating a Set that contains the unique elements in the stream.

Conclusion

Finding duplicate elements in Java 8 streams can be done using a variety of methods. The Collectors.groupingBy() method is useful for small streams, while the Set.add() method is more efficient for larger streams. Both methods are easy to use and can help you quickly identify duplicates in your stream data.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information