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
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 includeArraysfor converting an array to a list,HashSetandSetfor handling unique elements,Listfor the initial data structure, andCollectorsfor performing terminal operations on streams.List<Integer> integerList = Arrays.asList(12, 12, 3, 2, 1, 10, 30, 9, 10);: AListnamedintegerListis created usingArrays.asList()and initialized with the integers12, 12, 3, 2, 1, 10, 30, 9, 10. This list contains duplicate elements (12 and 10).Set<Integer> set = new HashSet<Integer>();: An emptyHashSetnamedsetis created. ThisHashSetwill be used to store unique elements encountered during the stream processing.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 theintegerList. 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 expressione -> !set.add(e)is a predicate that evaluates totruefor duplicate elements.set.add(e): Attempts to add the current elementeto theset. GeeksforGeeks explains that theadd()method of aHashSetreturnstrueif the element was successfully added (meaning it's a new, unique element), andfalseif the element already exists in the set.!set.add(e): The negation operator!means the predicate returnstrueonly ifset.add(e)returnsfalse(i.e., the elementeis already present in theset, making it a duplicate).
.collect(Collectors.toSet()): This is a terminal operation that collects the elements that passed the filter (the duplicates) into a newSetnamedduplicateElements. TheCollectors.toSet()method automatically handles duplicate elements within the collectedSet, ensuring that only unique duplicate elements are stored.
System.out.println(duplicateElements);: Prints theduplicateElementsset to the console. The output will show the unique duplicate elements found in the initial list.
Output
[10, 12]