Java HashSet containsAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

In Java, containsAll(c) returns true when a HashSet contains every element of the collection c. In set language, it asks "is c a subset of this set?".

It stops at the first missing element. The order and the duplicates in c do not matter, and an empty collection always gives true.

An instance method of java.util.HashSet (class since Java 1.2): a set of unique elements kept in a hash table, with no guaranteed order, one null element allowed, and no thread safety.

Syntax

One argument: the collection of required elements.

Java
Set<String> have = new HashSet<>(Arrays.asList("read", "write", "share"));
boolean ok = have.containsAll(Arrays.asList("read", "write"));   // true
boolean no = have.containsAll(Arrays.asList("read", "admin"));   // false
CallWhat it does
big.containsAll(small)true if every element of small is in big.
set.containsAll(Arrays.asList(...))Test a few literals without building a set.
a.containsAll(b) && b.containsAll(a)Both directions: the two sets hold the same elements.
set.containsAll(emptyList)Always true.

Direction matters: big.containsAll(small) can be true while small.containsAll(big) is false.

Library declarations
Java
// Inherited from java.util.AbstractCollection<E>
public boolean containsAll(Collection<?> c)

// Declared in Set<E> and Collection<E>

Parameters

ParameterDescription
c (Collection<?>)The elements to look for. It must not be null.

Return Value

Return ValueDescription
trueEvery element of c is in the set, or c is empty.
falseAt least one element of c is missing.

Compare with Similar Methods

Related checks and set operations:

MethodHow it differs
contains(e)Checks one element.
equals(o)True when the sizes match and containsAll is true.
retainAll(c)Keeps the shared part instead of just testing it.
removeAll(c)Removes the shared part.

Java Examples

Example 1 — Check That a User Has All Required Permissions

Compare the permissions a user holds with the permissions two actions need.

Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class HashSetContainsAllDemo {
    public static void main(String[] args) {
        Set<String> granted = new HashSet<>(Arrays.asList("read", "write", "share"));

        List<String> editNeeds = Arrays.asList("read", "write");
        List<String> adminNeeds = Arrays.asList("read", "admin");

        System.out.println("can edit: " + granted.containsAll(editNeeds));
        System.out.println("can administer: " + granted.containsAll(adminNeeds));
    }
}

How It Works

  1. Both present. "read" and "write" are both in granted, so the edit check passes.
  2. One missing is enough. "admin" was never granted, so the second check fails even though "read" matches.
  3. Lists work as arguments. The needs are plain List objects; only the receiving object has to be a set.
  4. Extra rights are fine. "share" is not needed by either action, and containsAll() does not care about it.

Example 2 — Empty, Repeated, and null Arguments

See what containsAll() does with an empty list, repeated values, a missing value, and null.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class HashSetContainsAllEdge {
    public static void main(String[] args) {
        Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3));

        List<Integer> empty = new ArrayList<>();
        System.out.println("empty list: " + set.containsAll(empty));
        System.out.println("repeats: " + set.containsAll(Arrays.asList(1, 1, 1, 3)));
        System.out.println("one missing: " + set.containsAll(Arrays.asList(1, 2, 3, 4)));

        try {
            set.containsAll(null);
        } catch (NullPointerException e) {
            System.out.println("null -> NullPointerException");
        }
    }
}

How It Works

  1. Nothing required. An empty list has no element that could be missing, so the answer is true.
  2. Repeats are harmless. The list asks for 1 three times and 3 once; every lookup succeeds, so the result is true.
  3. 4 breaks it. The set holds only 1, 2, and 3, so asking for 4 gives false.
  4. A null collection is a bug. There is nothing to iterate over, so the call throws NullPointerException.

Example 3 — Subset, Superset, and Equal Sets

Test the same two sets in both directions, and then test two sets that hold the same elements.

Java
import java.util.HashSet;
import java.util.Set;

public class HashSetContainsAllDirection {
    public static void main(String[] args) {
        Set<Integer> big = new HashSet<>();
        for (int i = 1; i <= 5; i++) {
            big.add(i);
        }
        Set<Integer> small = new HashSet<>();
        small.add(2);
        small.add(4);

        System.out.println("big has all of small: " + big.containsAll(small));
        System.out.println("small has all of big: " + small.containsAll(big));

        Set<Integer> copy = new HashSet<>(big);
        boolean same = big.containsAll(copy) && copy.containsAll(big);
        System.out.println("big and copy hold the same elements: " + same);
    }
}

How It Works

  1. Superset to subset. big holds 1 to 5, so it contains both 2 and 4 and the first check is true.
  2. Subset to superset. small has only two elements, so it cannot contain 1, 3, or 5 and the second check is false.
  3. Both directions at once. copy was built from big, so each set contains all of the other and the combined test is true.
  4. Same idea as equals(). Two-way containsAll is what equals() checks after it has compared the sizes.

Frequently Asked Questions

It returns true when the set contains every element of the collection you pass in, which makes it a subset test.
Call big.containsAll(small). It is true when every element of small is also in big.
true. There is no element that could be missing.
No. containsAll() only asks whether each element is present, so order and repeated values make no difference.
Yes. containsAll(null) throws NullPointerException. Check for null first if the collection can be missing.

Next: equals()

Compare two sets by their contents.

equals() 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.

12 people found this page helpful