Home Java Collections Methods disjoint() Java Collections disjoint() Method Definition In Java, Collections.disjoint(c1, c2) returns true if the two collections have no elements in common , and false as soon as it finds one element that is in both.
It answers "do these overlap?" without building an intersection. Typical uses are checking for scheduling conflicts, banned words, or duplicate IDs between two batches.
A static method of the java.util.Collections utility class, available since Java 5. Call it on the class name, as in Collections.disjoint(...), after import java.util.Collections;. You never create a Collections object.
Syntax Pass the two collections to compare. Their order does not affect the answer.
List<String> a = Arrays.asList("Mon", "Wed");
List<String> b = Arrays.asList("Tue", "Thu");
boolean free = Collections.disjoint(a, b); // true: no shared day Call What it does Collections.disjoint(a, b)true when no element is in both.!Collections.disjoint(a, b)true when they share at least one element.copy.retainAll(b)Builds the actual common elements (changes copy). a.containsAll(b)Asks the opposite kind of question: is all of b inside a?
Internally it loops over one collection and calls contains on the other. When one argument is a Set, it uses that set for the contains calls, so passing a set makes the check fast.
Library declarations // java.util.Collections
public static boolean disjoint(Collection<?> c1, Collection<?> c2) Parameters Parameter Description c1The first collection. c2The second collection.
Return Value Return Value Description trueNo element is in both collections (also when either one is empty). falseAt least one element is in both.
Important
Passing null for either collection throws NullPointerException. Elements are matched by each collection's own contains, so a TreeSet with a custom comparator may treat values as equal that equals would not. Both collections are only read, never changed.
Compare with Similar Methods Ways to compare the contents of two collections:
Method How it differs retainAll(c)Keeps only common elements, so you can see them; modifies the receiver. containsAll(c)Checks whether one collection contains all elements of another. frequency(c, o)Counts one value inside one collection. indexOfSubListLooks for a run of elements in order.
Java Examples 1. Schedule clash 2. Banned tags 3. Edge cases Example 1 — Check Two Schedules for a Clash Compare the days three people are busy and report whether each pair can share a room.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsDisjointSchedule {
public static void main(String[] args) {
List<String> ana = Arrays.asList("Mon", "Wed", "Fri");
List<String> ben = Arrays.asList("Tue", "Thu");
List<String> cy = Arrays.asList("Fri", "Sat");
System.out.println("Ana and Ben never clash: " + Collections.disjoint(ana, ben));
System.out.println("Ana and Cy never clash: " + Collections.disjoint(ana, cy));
}
} Ana and Ben never clash: true
Ana and Cy never clash: false How It Works No shared day. None of Ben's days appears in Ana's list, so the result is true.One overlap is enough. Ana and Cy are both busy on Fri, so disjoint returns false right away.Order does not matter. disjoint(cy, ana) would give the same answer.Example 2 — Detect Banned Tags, Then List Them Use disjoint as a quick yes-or-no check, and retainAll on a copy to see which tags caused it.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class CollectionsDisjointBanned {
public static void main(String[] args) {
Set<String> banned = new TreeSet<>(Arrays.asList("ads", "spam"));
List<String> tags = Arrays.asList("java", "spam", "tips");
if (!Collections.disjoint(tags, banned)) {
Set<String> hits = new TreeSet<>(tags);
hits.retainAll(banned);
System.out.println("Rejected, banned tags: " + hits);
} else {
System.out.println("Accepted");
}
}
} Rejected, banned tags: [spam] How It Works A set for lookups. banned is a TreeSet, so disjoint can use its fast contains.Overlap found. "spam" is in both collections, so disjoint is false and the if branch runs.Which ones? retainAll on a copy keeps only tags that are also banned, giving [spam] without changing the original list.Example 3 — Empty, Same, and null Collections Try an empty collection, a collection compared with itself, lists that both hold null, and a null argument.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsDisjointEdges {
public static void main(String[] args) {
List<Integer> empty = new ArrayList<>();
List<Integer> nums = Arrays.asList(1, 2);
System.out.println("empty vs nums: " + Collections.disjoint(empty, nums));
System.out.println("nums vs nums: " + Collections.disjoint(nums, nums));
System.out.println("null in both: " + Collections.disjoint(Arrays.asList("a", null), Arrays.asList(null, "b")));
try {
Collections.disjoint(nums, null);
} catch (NullPointerException e) {
System.out.println("null argument: " + e.getClass().getSimpleName());
}
}
} empty vs nums: true
nums vs nums: false
null in both: false
null argument: NullPointerException How It Works Empty never overlaps. A collection with no elements cannot share one, so the answer is true.Itself. A non-empty collection shares every element with itself, so comparing nums with nums gives false.null is an element too. Both lists contain null, and ArrayList-style contains(null) finds it, so they are not disjoint.No collection at all. Passing null instead of a collection throws NullPointerException.Frequently Asked Questions What does Collections.disjoint() return? true if the two collections have no elements in common, false if at least one element is in both.
Is disjoint() true when one collection is empty? Yes. An empty collection shares nothing with any other collection.
How do I get the common elements instead of true or false? Copy one collection and call retainAll with the other: the copy then holds only the common elements.
Does Collections.disjoint() change the collections? No. It only reads them.
Is Collections.disjoint() fast? It is fastest when one argument is a HashSet or TreeSet, because it uses that set for the contains checks.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion