You can use Collections.frequency:
numbers.stream().filter(i -> Collections.frequency(numbers, i) >1)
.collect(Collectors.toSet()).forEach(System.out::println);
Answer from Bao Dinh on Stack Overflow Top answer 1 of 16
155
You can use Collections.frequency:
numbers.stream().filter(i -> Collections.frequency(numbers, i) >1)
.collect(Collectors.toSet()).forEach(System.out::println);
2 of 16
111
Basic example. First-half builds the frequency-map, second-half reduces it to a filtered list. Probably not as efficient as Dave's answer, but more versatile (like if you want to detect exactly two etc.)
List<Integer> duplicates = IntStream.of( 1, 2, 3, 2, 1, 2, 3, 4, 2, 2, 2 )
.boxed()
.collect( Collectors.groupingBy( Function.identity(), Collectors.counting() ) )
.entrySet()
.stream()
.filter( p -> p.getValue() > 1 )
.map( Map.Entry::getKey )
.collect( Collectors.toList() );
Mkyong
mkyong.com โบ home โบ java8 โบ java 8 โ find duplicate elements in a stream
Java 8 โ Find duplicate elements in a Stream | mkyong.com
March 15, 2020 - In Java 8 Stream, filter with Set.Add() is the fastest algorithm to find duplicate elements, because it loops only one time. Set<T> items = new HashSet<>(); return list.stream() .filter(n -> !items.add(n)) .collect(Collectors.toSet()); The ...
TutorialsPoint
tutorialspoint.com โบ how-to-find-duplicate-elements-in-a-stream-in-java
How to find duplicate elements in a Stream in Java
July 19, 2023 - The Collections.frequency() method will count the occurrences of each element in the stream and then collects the elements with a count greater than one into a Set to remove duplicates. The resulting Set will contain only the duplicate elements from the stream. import java.util.stream.*; import ...
Stack Abuse
stackabuse.com โบ java-finding-duplicate-elements-in-a-stream
Java: Finding Duplicate Elements in a Stream
September 20, 2023 - Here, we try to add() each element to the Set. If it's not added, due to it being duplicate, we collect that value and print it out: ... Alternatively, you can also count the occurrences of duplicate elements and keep that information in a map that contains the duplicate elements as keys and their frequency as values. ... Map<Integer, Integer> map = list.stream() .collect(Collectors.toMap(Function.identity(), value -> 1, Integer::sum)); System.out.println(map);
Stack Abuse
stackabuse.com โบ guide-to-java-8-collectors-toset
Guide to Java 8 Collectors: toSet()
May 19, 2023 - Set<String> booksGeorgeOrwell = ... Method and Functional Programming in Java 8: Definitive Guide to Predicates! As we discussed earlier, Sets don't allow for duplicates....
Java67
java67.com โบ 2018 โบ 06 โบ how-to-remove-duplicates-from-stream-in-java8-distinct-example.html
How to remove duplicates from Collections or Stream in Java? Stream distinct() Example | Java67
*/ public class Hello { public static void main(String args[]) { List<Integer> withDupes = Arrays.asList(10, 10, 20, 20, 30, 30, 40, 50); System.out.println("List with duplicates: " + withDupes); List<Integer> withoutDupes = withDupes.stream() .distinct() .collect(Collectors.toList()); System.out.println("List without duplicates: " + withoutDupes); } } Output List with duplicates: [10, 10, 20, 20, 30, 30, 40, 50] List without duplicates: [10, 20, 30, 40, 50] You can see that the resulting List doesn't have a duplicate because we have collected it from the Stream after calling the distinct method, which removes the duplicate elements 10, 20, and 30. In other words, this is a new way to remove duplicate elements from a List in Java.
DEV Community
dev.to โบ realnamehidden1_61 โบ java-streamdistinct-495k
Java Stream.distinct() - DEV Community
November 17, 2024 - import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { // List of names with duplicates List<String> names = List.of("Alice", "Bob", "Alice", "Charlie", "Bob", "David"); // Use Stream.distinct() to remove duplicates List<String> uniqueNames = names.stream() .distinct() .collect(Collectors.toList()); System.out.println(uniqueNames); // Output: [Alice, Bob, Charlie, David] } }
GeeksforGeeks
geeksforgeeks.org โบ java โบ how-to-find-duplicate-elements-in-a-stream-in-java
How to find duplicate elements in a Stream in Java - GeeksforGeeks
July 12, 2025 - ... For each element in the stream, if it is not present in the set, add it. This can be done using Set.add() method. ... If the element is present in the Set already, then this Set.add() returns false.
YouTube
youtube.com โบ programming with chetan
Java 8 Stream API | How to find duplicate element from List using Java 8 | Lambda Expression in Java - YouTube
#Java8Features #StreamAPI #LambdaExpressionBy watching this tutorial you can find duplicate items from List using 3 different ways using Java 8 stream API fe...
Published: July 31, 2022
Views: 3K
HowToDoInJava
howtodoinjava.com โบ home โบ java 8 โบ java stream distinct(): collect unique values
Java Stream distinct(): Collect Unique Values
July 21, 2024 - ... Added in Java 8, the Stream.distinct() method returns a new Stream consisting of the distinct elements from the given Stream. The distinct() operation removes duplicate elements from a stream, ensuring that only unique elements are retained ...
Javahandson
javahandson.com โบ home โบ courses โบ java 8 โบ collecting data with streams โบ java 8 stream collect to list, set, and map: tolist, toset, and tomap
Java 8 Stream collect to List, Set, and Map: toList, toSet, and toMap - Java-HandsOn
2 weeks ago - You also get toUnmodifiableSet and toUnmodifiableMap. So you can lock down any of the three when needed. A Set is the right choice when you want unique items. It drops any repeats for you. Therefore, it is perfect for removing duplicates from a stream. The toSet collector works just like toList.