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
๐ŸŒ
Medium
medium.com โ€บ @premchandu.in โ€บ finding-duplicates-using-java-streams-af97bc106cfc
Finding Duplicates using java streams | by Premchandu | Medium
March 3, 2024 - For each element in the stream, ... method.Then for each element in the collected map, if the frequency of any element is more than one, then this element is a duplicate element....
Discussions

simplify java stream to find duplicate properties - Stack Overflow
If someone can find improvements then please share. ... Nit: you have a Set in your original code, a List here. 2020-04-03T12:23:39.483Z+00:00 ... The complexity is O(n) in typical implementations, see Complexity of grouping in Java8. You can even simplify the counting to only keep a boolean denoting whether it is a duplicate... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to check if exists any duplicate in Java 8 Streams? - Stack Overflow
โ€ข Performance benefit from ... where duplicates are really a possibility, we typically not only check for their presence but also to find and handle them. โ€ข M. Justin's provides a clean (not using side-effects) short-circuiting solution for Java 22, but that's not a one-liner. ... M. Justin Over a year ago ยท #3 operates via side effects, which is potentially problematic with streams... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to find duplicate values in a stream without loading in memory
Take a look at bloom filters. Worth the right tuning, you could dedupe with a fairly high confidence level. More on reddit.com
๐ŸŒ r/scala
11
4
December 28, 2018
HashSet containing duplicates!?
http://stackoverflow.com/questions/16633149/avoid-duplicate-records-hashset From what I read, you need to override the .equals() method. More on reddit.com
๐ŸŒ r/javahelp
11
5
May 12, 2016
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ finding all duplicates in a list in java
Finding All Duplicates in a List in Java | Baeldung
March 7, 2025 - This implementation offers a more concise and modern approach to finding duplicates in an array. In this article, we learned about different ways of extracting duplicate elements from a List in Java. We discussed approaches using Set and Map and their corresponding approaches using Stream.
๐ŸŒ
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.
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java 8 โ€บ java stream โ€“ find, count and remove duplicates
Java Stream - Find, Count and Remove Duplicates
July 17, 2022 - //Add some random persons ... using a different equality condition, we can take the help of the following distinctByKey() method....
๐ŸŒ
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, count the frequency of each element, using Collections.frequency() method. ... Then for each element in the collection list, if the frequency of any element is more than one, then this element is a duplicate element.
๐ŸŒ
DEV Community
dev.to โ€บ realnamehidden1_61 โ€บ java-streamdistinct-495k
Java Stream.distinct() - DEV Community
November 17, 2024 - The Stream.distinct() method in Java is used to filter out duplicate elements from a stream, ensuring that the resulting stream contains only unique elements.
Find elsewhere
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ java-finding-duplicate-elements-in-a-stream
Java: Finding Duplicate Elements in a Stream
September 20, 2023 - list.stream() .distinct() .collect(Collectors.toList()) .forEach(System.out::print); ... In this article, we've gone over a few approaches to finding duplicate elements in a Java Stream.
๐ŸŒ
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 - Java provides several ways to find duplicate elements, we will focus mainly on two ways: the first one is by using Set of Java Collection Framework and the other one is by using the built-in method of stream named Collections.frequency().
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ core java โ€บ java 8 โ€บ java 8 โ€“ find duplicate elements in stream
Java 8 โ€“ Find duplicate elements in Stream - Java2Blog
October 16, 2021 - To view the duplicate elements, we just remove the elements in the new List from the original List, and we will be left with the duplicate elements. ... Create a List of employee objects and call the stream() method to return their a Stream.
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ Find-duplicates-in-a-List-in-Java
Find duplicates in a List in Java
The later convenience method ... = new HashSet<Integer>(); var duplicates = numbers.stream() .filter(number -> !seen.add(number)) .collect(Collectors.toList()); System.out.println(duplicates);...
Published: February 10, 2026
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-stream-distinct-function
Java Stream distinct() Function to Remove Duplicates | DigitalOcean
Technical tutorials, Q&A, events โ€” This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ find-duplicate-strings-collection-using-java-streams-mohan-k
Find duplicate strings in a collection using Java streams
July 11, 2023 - 4. Collect the duplicate elements using `Collectors.toList()`. ... List<String> fruits = Arrays.asList("apple", "orange", "banana", "apple", "mango", "orange", "kiwi"); ... Mark contributions as unhelpful if you find them irrelevant or not valuable ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Top 50+ Java Streams Coding Questions - How to find duplicate elements in a given integers list - YouTube
๐Ÿ” Find Duplicate Elements in a List of Integers Using Java Streams | Java 8 Stream API Tutorial ๐Ÿ”In this video, youโ€™ll learn how to identify and extract du...
Published: May 13, 2025
๐ŸŒ
Java Guides
javaguides.net โ€บ 2024 โ€บ 09 โ€บ java-8-find-duplicate-elements-in-stream.html
Java 8 โ€“ Find Duplicate Elements in a Stream
September 9, 2024 - In Java 8, finding duplicate elements in a Stream is easy and efficient using the filter() method and a Set to track previously seen elements.
Top answer
1 of 7
77

Your code would need to iterate over all elements. If you want to make sure that there are no duplicates simple method like

public static <T> boolean areAllUnique(List<T> list){
    Set<T> set = new HashSet<>();

    for (T t: list){
        if (!set.add(t))
            return false;
    }
    
    return true;
}

would be more efficient since it can give you false immediately when first non-unique element would be found.

This method could also be rewritten using Stream#allMatch which also is short-circuit (returns false immediately for first element which doesn't fulfill provided condition)
(assuming non-parallel streams and thread-safe environment)

public static <T> boolean areAllUnique(List<T> list){
    Set<T> set = new HashSet<>();
    return list.stream().allMatch(t -> set.add(t));
}

which can be farther shortened as @Holger pointed out in comment

public static <T> boolean areAllUnique(List<T> list){
    return list.stream().allMatch(new HashSet<>()::add);
}
2 of 7
23

Out of the three following one-liners

  1. return list.size() == new HashSet<>(list).size();
  2. return list.size() == list.stream().distinct().count();
  3. return list.stream().allMatch(ConcurrentHashMap.newKeySet()::add);

the last one (#3) has the following benefits:

  • Performance: it short-circuits, i.e. it stops on the first duplicate (while #1 and #2 always iterate till the end) โ€“ as Pshemo commented.
  • Versatility: it allows to handle not only collections (e.g. lists), but also streams (without explicitly collecting them).

Additional notes:
 โ€ข ConcurrentHashMap.newKeySet() is a way to create a concurrent hash set.
 โ€ข Previous version of this answer suggested the following code: return list.stream().sequential().allMatch(new HashSet<>()::add); โ€” but, as M. Justin commented, calling BaseStream#sequential() doesn't guarantee that the operations will be executed from the same thread and doesn't eliminate the need for synchronization.
 โ€ข Performance benefit from short-circuiting won't usually matter, because we typically use such code in assertions and other correctness checks, where items are expected to be unique; in contexts where duplicates are really a possibility, we typically not only check for their presence but also to find and handle them.
 โ€ข M. Justin's provides a clean (not using side-effects) short-circuiting solution for Java 22, but that's not a one-liner.

๐ŸŒ
Reddit
reddit.com โ€บ r/scala โ€บ how to find duplicate values in a stream without loading in memory
r/scala on Reddit: How to find duplicate values in a stream without loading in memory
December 28, 2018 -

I am working with a large stream and need to find out whether any duplicate values exist in the stream. I can do this when the stream is small by loading everything in a `Seq` and calling distinct however this doesn't work with large streams since I can't hold everything in memory.

Here is an example:

I have a source like this:

1 | red | light | 10

2 | blue | dark | 20

1 | brown | light | 2

1 | red | light | 10

20 | grey | dark | 200

I want to find out (`true / false`) whether there are any identical items in the source. In the above stream `1 | red | light | 10` would be identical. This stream could be very big over 2M records. I can return `true` soon as the identical item is found (i.e. in the example above, we can avoid reading `20 | grey | dark | 200`). What is the best way to do this? I tried reading the entire source into a `List(String)` and ran distinct on it. This works ok, however, for large sources I start receiving a OOM error.

val restResult: Future[immutable.Seq[Color]] =

mySource(ctx)

.via(framing("\n"))

.map(_.utf8String)

.map(_.trim)

.map(s => ColorParser(s))

.collect {

case Right(color) => color

}

.runWith(Sink.seq)

๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java 8 โ€บ java stream distinct(): collect unique values
Java Stream distinct(): Collect Unique Values
July 21, 2024 - The primary purpose of Stream.distinct() is to eliminate duplicate elements from a given stream, guaranteeing that only distinct elements remain in the resulting stream.
๐ŸŒ
Medium
gainjavaknowledge.medium.com โ€บ how-to-find-duplicate-elements-from-string-using-java8-dfb3516818ef
How to find duplicate elements from string using Java8? | by Real World Java | Medium
August 6, 2023 - In this tutorial, we will learn How to filter out duplicate characters in Java 8. First we will split string based on โ€œโ€ empty string value and will store into Array of string and after that we will use Arrays.Stream(-) method and convert ...