In this tutorial, we will see “How to find Duplicate Element in a Stream in Java 8?”. In order to find duplicates, we are going to use several techniques. Find Duplicate Elements in a Stream in Java 8...!!! Share on X
1. Find Duplicate Elements using ‘Set’
Set doesn’t allow duplicates and therefore return ‘false’ if the element is already present.
Example
/**
* Find Duplicate Elements in a Stream using Set
* @author Deepak Verma
*
*/
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.*;
public class Find_Duplicate_Element_In_Stream_Using_Set {
public static void main(String[] args)
{
//Stream of Integers that may or may not contain Duplicate Elements
Stream streamOfIntegers = Stream.of(2, 52, 5, 3, 21, 7, 99, 76, 21, 15, 34, 9, 21, 3, 81, 34, 1, 99);
//Print the list of Duplicate Elements Preset in the above Stream
System.out.println("Duplicate Elements preset in the Stream: "+ findDuplicateElementInStream(streamOfIntegers));
}
public static Set findDuplicateElementInStream(Stream stream)
{
//Create Set to store the duplicate elements
Set setOfElements = new HashSet();
//Set return false every time a newly added element is already present in the Set. And, then filter those duplicate elements
Predicate filterDuplicateIntegers = x -> !setOfElements.add(x);
//Apply the filter created above to the Original Stream and then Collect them as a new Set
return stream.filter(filterDuplicateIntegers)
.collect(Collectors.toSet());
}
}
Output:
Duplicate Elements preset in the Stream: [34, 3, 99, 21]
2. Find Duplicate Elements using ‘Collectors.groupingBy()’
Group the elements in a Map using Collectors.groupingBy and find elements if their occurrence is more than 1
Example
/**
* Find Duplicate Elements in a Stream using Collectors.groupingBy
* @author Deepak Verma
*
*/
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Find_Duplicate_Element_In_Stream_Using_Collectors_GroupingBy {
public static void main(String[] args)
{
//Stream of Integers that may or may not contain Duplicate Elements
Stream streamOfIntegers = Stream.of(2, 52, 5, 3, 21, 7, 99, 76, 21, 15, 34, 9, 21, 3, 81, 34, 1, 99);
//Print the list of Duplicate Elements Preset in the above Stream
System.out.println("Duplicate Elements preset in the Stream: "+ findDuplicateElementInStream(streamOfIntegers));
}
public static Set findDuplicateElementInStream(Stream stream)
{
//return the set of duplicate elements
return stream
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting())) //Create the map and group them with the frequency of their occurrence
.entrySet().stream() //Convert the map to the stream
.filter(m -> m.getValue() > 1) //Check if the occurrence of the element is more than 1
.map(Map.Entry::getKey).collect(Collectors.toSet()); //Find such elements and store them in a Set
}
}
Output:
Duplicate Elements preset in the Stream: [34, 99, 3, 21]
3. Find Duplicate Elements using ‘Collections.frequency()’
Filter the elements from the List whose occurrence is more than 1 using Collections.frequency() and then count those elements
Example
/**
* Find Duplicate Elements in a Stream using Collections.frequency
* @author Deepak Verma
*
*/
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Find_Duplicate_Element_In_Stream_Using_Collections_Frequency {
public static void main(String[] args)
{
//Create a list of elements
var listOfIntegers = Arrays.asList(2, 52, 5, 3, 21, 7, 99, 76, 21, 15, 34, 9, 21, 3, 81, 34, 1, 99);
//Print the list of Duplicate Elements
System.out.println("Duplicate Elements preset in the Stream: "+ findDuplicateElementInStream(listOfIntegers));
}
public static Set findDuplicateElementInStream(List list)
{
//return the set of duplicate elements
return list.stream() //Convert the list into a Stream
.filter(x -> Collections.frequency(list, x) > 1) //Filter the elements whose occurrence are greater than 1 and count those elements
.collect(Collectors.toSet()); //Store those elements into a set
}
}
Output:
Duplicate Elements preset in the Stream: [34, 3, 99, 21]
Find Duplicate Elements in a Stream in Java 8...!!! Share on X Do you like this Post? – then check my other helpful posts:
- Double the even / odd numbers of a specified ArrayList using Streams
- Double the numbers of specified ArrayList using Streams