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
Call
What 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>
Nothing required. An empty list has no element that could be missing, so the answer is true.
Repeats are harmless. The list asks for 1 three times and 3 once; every lookup succeeds, so the result is true.
4 breaks it. The set holds only 1, 2, and 3, so asking for 4 gives false.
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);
}
}
Output
big has all of small: true
small has all of big: false
big and copy hold the same elements: true
How It Works
Superset to subset.big holds 1 to 5, so it contains both 2 and 4 and the first check is true.
Subset to superset.small has only two elements, so it cannot contain 1, 3, or 5 and the second check is false.
Both directions at once.copy was built from big, so each set contains all of the other and the combined test is true.
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.