R4R Style Learning Portal

Extract duplicate objects from a list in java 8 stream API

The provided Java code, Extact Duplicate Elements using Java8 Stream API , efficiently identifies and extracts duplicate elements from a List of integers using a combination of Java Streams and a HashSet.  
 
Here's a detailed explanation of the code:extract duplicate objects from a list in java 8
 
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ExtactDuplicateElements {
    public static void main(String[] str){
        // 1. Create a list of integers with duplicates
        List<Integer> integerList= Arrays.asList(12,12,3,2,1,10,30,9,10);
        
        // 2. Initialize a HashSet to keep track of unique elements encountered
        Set<Integer> set= new HashSet<Integer>();
        
        // 3. Use Stream API to filter and collect duplicate elements
        Set<Integer> duplicateElements=integerList.stream()
                                            .filter(e->!set.add(e)) // Filter elements that are already in 'set'
                                            .collect(Collectors.toSet()); // Collect the filtered (duplicate) elements into a Set
        
        // 4. Print the set of duplicate elements
        System.out.println(duplicateElements);
    }
}
 
Explanation
  1. import java.util.Arrays;, import java.util.HashSet;, import java.util.List;, import java.util.Set;, import java.util.stream.Collectors;: These lines import the necessary classes and interfaces from the Java utility and stream packages. These include Arrays for converting an array to a list, HashSet and Set for handling unique elements, List for the initial data structure, and Collectors for performing terminal operations on streams.
  2. List<Integer> integerList = Arrays.asList(12, 12, 3, 2, 1, 10, 30, 9, 10);: A List named integerList is created using Arrays.asList() and initialized with the integers 12, 12, 3, 2, 1, 10, 30, 9, 10. This list contains duplicate elements (12 and 10).
  3. Set<Integer> set = new HashSet<Integer>();: An empty HashSet named set is created. This HashSet will be used to store unique elements encountered during the stream processing.
  4. Set<Integer> duplicateElements = integerList.stream().filter(e -> !set.add(e)).collect(Collectors.toSet());: This is the core logic for identifying duplicate elements.
    • integerList.stream(): Creates a stream of integers from the integerList. JRebel notes that Java 8 Stream API enables processing data in a functional style.
    • .filter(e -> !set.add(e)): This is an intermediate operation that filters the stream. The lambda expression e -> !set.add(e) is a predicate that evaluates to truefor duplicate elements.
      • set.add(e): Attempts to add the current element e to the set. GeeksforGeeks explains that the add() method of a HashSet returns true if the element was successfully added (meaning it's a new, unique element), and false if the element already exists in the set.
      • !set.add(e): The negation operator ! means the predicate returns true only if set.add(e) returns false (i.e., the element e is already present in the set, making it a duplicate).
    • .collect(Collectors.toSet()): This is a terminal operation that collects the elements that passed the filter (the duplicates) into a new Set named duplicateElements. The Collectors.toSet() method automatically handles duplicate elements within the collected Set, ensuring that only unique duplicate elements are stored.
  5. System.out.println(duplicateElements);: Prints the duplicateElements set to the console. The output will show the unique duplicate elements found in the initial list.
 
Output
 
[10, 12]