In Java, removeAll(c) removes from a HashSet every element that is also contained in the collection c. For two sets a and b, a.removeAll(b) leaves the difference: the elements of a that are not in b.
It returns true if at least one element was removed. The argument is only read; it can be a set, a list, or any other collection.
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 elements to take out.
Java
Set<Integer> a = new HashSet<>(Arrays.asList(1, 2, 3, 4));
a.removeAll(Arrays.asList(3, 4, 9));
System.out.println(a.size()); // 2
Call
What it does
a.removeAll(b)
Keeps only the elements of a that are not in b.
a.removeAll(Arrays.asList(...))
Takes out a few listed values.
new HashSet<>(a).removeAll(b)
Computes the difference on a copy, so a stays intact.
a.removeAll(a)
Removes everything, like clear().
If the set is larger than the argument, removeAll looks up each argument element in the set. Otherwise it asks the argument contains() for every set element, which is slow when the argument is a List; pass a set when the collections are big.
Library declarations
Java
// Inherited from java.util.AbstractSet<E>
public boolean removeAll(Collection<?> c)
// Declared in Set<E> and Collection<E>
Parameters
Parameter
Description
c (Collection<?>)
The elements to remove from this set. It must not be null.
Return Value
Return Value
Description
true
The set changed because at least one element was removed.
first call: true
second call: false
left: [10, 30, 50]
How It Works
A list is accepted. The parameter is a Collection, so soldOut can stay a plain List.
Repeats and strangers are fine. The second 40 is already gone by the time it is reached, and 99 was never in the set; neither causes an error.
First call true, second false. The first call removes 20 and 40. When the same list is used again, nothing matches, so the set does not change.
What remains. The three untouched codes 10, 30, and 50 are printed in sorted order.
Example 3 — Who Is Only in One Group? Direction Matters
Subtract two overlapping sets in both directions and combine the results into a symmetric difference.
Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class HashSetRemoveAllDirection {
public static void main(String[] args) {
Set<Integer> a = new HashSet<>(Arrays.asList(1, 2, 3, 4));
Set<Integer> b = new HashSet<>(Arrays.asList(3, 4, 5, 6));
Set<Integer> onlyA = new HashSet<>(a);
onlyA.removeAll(b);
Set<Integer> onlyB = new HashSet<>(b);
onlyB.removeAll(a);
Set<Integer> either = new HashSet<>(onlyA);
either.addAll(onlyB);
System.out.println("only in a: " + new TreeSet<>(onlyA));
System.out.println("only in b: " + new TreeSet<>(onlyB));
System.out.println("in exactly one: " + new TreeSet<>(either));
}
}
Output
only in a: [1, 2]
only in b: [5, 6]
in exactly one: [1, 2, 5, 6]
How It Works
a minus b. 3 and 4 are in both sets, so removing b's elements from a copy of a leaves 1 and 2.
b minus a. The mirror-image call leaves 5 and 6, which proves that swapping the two sets changes the answer.
Combine the leftovers.addAll merges the two differences into the elements that belong to exactly one of the sets.
Shared values drop out. 3 and 4 appear in neither difference, so they are missing from the final line.
Frequently Asked Questions
It removes every element that is also in the collection you pass in and returns true if the set changed.
Copy the first set with new HashSet<>(a), then call copy.removeAll(b). The copy holds the elements that are in a but not in b.
No. clear() empties the whole set. removeAll(c) removes only the elements that appear in c.
When the set is not larger than the argument, removeAll() calls contains() on the argument for every element. A list scans linearly, so pass a HashSet for big collections.