Java Collections disjoint() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

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.

Java
List<String> a = Arrays.asList("Mon", "Wed");
List<String> b = Arrays.asList("Tue", "Thu");
boolean free = Collections.disjoint(a, b);   // true: no shared day
CallWhat 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
// java.util.Collections
public static boolean disjoint(Collection<?> c1, Collection<?> c2)

Parameters

ParameterDescription
c1The first collection.
c2The second collection.

Return Value

Return ValueDescription
trueNo element is in both collections (also when either one is empty).
falseAt least one element is in both.

Compare with Similar Methods

Ways to compare the contents of two collections:

MethodHow 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

Example 1 — Check Two Schedules for a Clash

Compare the days three people are busy and report whether each pair can share a room.

Java
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));
    }
}

How It Works

  1. No shared day. None of Ben's days appears in Ana's list, so the result is true.
  2. One overlap is enough. Ana and Cy are both busy on Fri, so disjoint returns false right away.
  3. 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.

Java
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");
        }
    }
}

How It Works

  1. A set for lookups. banned is a TreeSet, so disjoint can use its fast contains.
  2. Overlap found. "spam" is in both collections, so disjoint is false and the if branch runs.
  3. 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.

Java
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());
        }
    }
}

How It Works

  1. Empty never overlaps. A collection with no elements cannot share one, so the answer is true.
  2. Itself. A non-empty collection shares every element with itself, so comparing nums with nums gives false.
  3. null is an element too. Both lists contain null, and ArrayList-style contains(null) finds it, so they are not disjoint.
  4. No collection at all. Passing null instead of a collection throws NullPointerException.

Frequently Asked Questions

true if the two collections have no elements in common, false if at least one element is in both.
Yes. An empty collection shares nothing with any other collection.
Copy one collection and call retainAll with the other: the copy then holds only the common elements.
No. It only reads them.
It is fastest when one argument is a HashSet or TreeSet, because it uses that set for the contains checks.

Next: emptyEnumeration()

Get a legacy Enumeration that has no elements (Java 7+).

emptyEnumeration() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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