Something like 'contains any' for Java set?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Java's Set interface does not have a built-in method literally named containsAny, but the operation is still simple. The question is really: do these two collections share at least one element? In Java, the cleanest answers are usually !Collections.disjoint(...), a short loop with early return, or a stream-based check when readability matters more than minimal overhead.
The Simplest Standard-Library Answer
The standard library already exposes the inverse operation: Collections.disjoint(a, b) returns true when two collections have no elements in common.
So a practical containsAny check is:
This is usually the best first answer because it is concise, standard, and immediately communicates intent.
Why a Manual Loop Is Still Useful
Sometimes you want the logic to be explicit, or you know one side is a HashSet and want to iterate over the smaller input with early exit.
This version has two advantages:
- it exits immediately on the first match
- it lets you control which collection drives the iteration
That second point can matter for performance if one collection is much smaller than the other.
Stream API Version
If your codebase leans on streams, anyMatch is a natural translation:
This reads well, especially in data-processing code, but it does not fundamentally do anything magical. It is still checking membership one value at a time.
Choose the Iteration Direction Deliberately
If both inputs are sets, the asymptotic idea is simple: iterate over the smaller set and call contains on the larger set.
With hash-based sets, this is usually close to optimal in practice.
When retainAll Is the Wrong Tool
Some developers reach for intersection logic:
This works, but it is often heavier than necessary because:
- it allocates a copy
- it may process more elements than needed
- it expresses "build the intersection" when the real need is only "does any overlap exist"
Use retainAll when you actually need the intersection set. If you only need a boolean, prefer disjoint, a loop, or anyMatch.
Null and Collection-Type Considerations
A Set can usually answer contains efficiently, but if the other side is a list or any generic iterable, you still have good options. The normal rule is:
- keep the hash-based membership test on the set side
- iterate over the other collection once
Also decide how your API treats null inputs. In production code, a helper should either reject them clearly or document how they are interpreted.
A Small Utility Method for Reuse
If the check appears repeatedly, wrap it in a helper and keep the semantics in one place.
This keeps call sites readable and prevents every module from inventing its own variation.
Common Pitfalls
The biggest mistake is building a full intersection set when you only need a boolean result.
Another mistake is ignoring collection size. If you can iterate over the smaller collection and test membership in the larger set, do that.
Developers also sometimes use streams for everything even when a small loop is clearer in the local context.
Finally, do not assume a method named containsAny exists on Set itself. The operation exists conceptually, but you express it through other APIs.
Summary
- Java
Sethas no method literally namedcontainsAny. - The clean standard-library answer is
!Collections.disjoint(a, b). - A manual loop or
anyMatchis also fine, especially when you want explicit control. - Iterate over the smaller collection when performance matters.
- Use
retainAllonly when you need the actual intersection, not just a boolean.
Related reading
- Sort 2 lists in Python based on the ratio of individual corresponding elements or based on a third list
- Sort a 2d array by a column value
- Sort a list alphabetically
- Sort a list by multiple attributes?
- SonarQube rule Using command line arguments is security-sensitive in Spring Boot application
- Sort a single String in Java
- Sort a list of tuples by 2nd item integer value
- Sort a list of two-sided items based on the similarity of consecutive items

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.