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() );
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);
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 ...
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 ...
Benchresources
benchresources.net › home › java › java 8 – how to find duplicate in a stream or list ?
Java 8 - How to find duplicate in a Stream or List ? - BenchResources.Net
October 13, 2022 - For finding duplicates, use ... duplicates present in the Original List · Finally, store those elements into another new Set using collect(Collectors.toSet()) method · package net.bench.resources.java.stream; import java.util.ArrayList; import java.util.Collections; import ...
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....
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] } }
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.
GeeksforGeeks
geeksforgeeks.org › java › convert-stream-set-java
Convert Stream to Set in Java - GeeksforGeeks
July 18, 2026 - Java provides the following ways to convert a Stream into a Set. The Collectors.toSet() method is the most commonly used way to collect stream elements into a Set. It automatically removes duplicate elements.
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 ...