Java 8 Streams - How to find the duplicate elements from a Collection - Multiple ways

Example 1: Using Collections.frequency()

To find frequency of a particular element we can use the frequency() method:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class DriverClass {
public static void main(String[] args) {

List<String> obj = Arrays.asList("ant", "1", "cat", "ant",
"we", "java", "cat");
obj.stream().filter(i -> Collections.
frequency(obj, i) > 1).collect(Collectors.toSet())
.forEach(System.out::println);

}
}

Output:

ant
cat



Example 2: Using Collectors.groupingBy()

We can also use the Collectors.groupingBy() method to count the frequency of elements present in a stream. 
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class DriverClass {
public static void main(String[] args) {
List<String> obj = Arrays.asList("ant", "1", "cat", "ant",
"we", "java", "cat");
Set<String> result = obj.stream().
collect(Collectors.groupingBy(Function.identity(),
Collectors.counting())).entrySet().
stream().filter(m -> m.getValue() > 1).
map(Map.Entry::getKey)
.collect(Collectors.toSet());
result.stream().forEach(System.out::println);

}
}

Output:

ant
cat



Example 3: Using Set.add()

If the element is present in the Set already, then this Set.add() returns false.
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class DriverClass {
public static void main(String[] args) {

List<String> obj = Arrays.asList("ant", "1", "cat", "ant",
"we", "java", "cat");
Set<String> items = new HashSet<>();
Set<String> result = obj.stream().
filter(n -> !items.add((String) n)).
collect(Collectors.toSet());
result.stream().forEach(System.out::println);
}
}

Output:
ant
cat



Example 4: Using Collectors.toMap()

public class DriverClass {

public static void main(String[] args) {

List<String> obj = Arrays.
asList("ant", "1", "cat",
"ant", "we", "java", "cat");
Map<String, Integer> map = obj.stream()
.collect(Collectors.
toMap(Function.identity(),
value -> 1, Integer::sum));

System.out.println(map);
}
}

Output:
{1=1, java=1, ant=2, cat=2, we=1}

More topics...

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String