Java HashSet removeAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

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
CallWhat 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

ParameterDescription
c (Collection<?>)The elements to remove from this set. It must not be null.

Return Value

Return ValueDescription
trueThe set changed because at least one element was removed.
falseNone of the elements of c were in the set.

Compare with Similar Methods

Set operations in one table:

MethodHow it differs
retainAll(c)The opposite: keeps only the shared elements (intersection).
addAll(c)Adds elements (union).
remove(o)Removes a single element.
removeIf(test)Removes by a condition instead of by membership.

Java Examples

Example 1 — Find the Guests Who Have Not Replied

Start from the invited names and remove everyone who has already answered.

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

public class HashSetRemoveAllDemo {
    public static void main(String[] args) {
        Set<String> invited = new HashSet<>();
        invited.add("Ann");
        invited.add("Bo");
        invited.add("Cy");
        invited.add("Di");

        Set<String> replied = new HashSet<>();
        replied.add("Bo");
        replied.add("Di");
        replied.add("Ed");

        Set<String> pending = new HashSet<>(invited);
        boolean changed = pending.removeAll(replied);

        System.out.println("changed: " + changed);
        System.out.println("pending: " + new TreeSet<>(pending));
        System.out.println("invited still has " + invited.size());
    }
}

How It Works

  1. Work on a copy. new HashSet<>(invited) gives pending its own four names, so the original list of invitations is kept.
  2. Two names match. Bo and Di appear in replied, so they are removed and the result is true.
  3. Ed is ignored. Ed replied but was never invited; removeAll only removes what it finds and never adds anything.
  4. Original untouched. invited still has four names, while pending holds Ann and Cy.

Example 2 — Remove Sold-Out Items Using a List

Pass a list that has repeats and an unknown value, then call the method a second time.

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

public class HashSetRemoveAllList {
    public static void main(String[] args) {
        Set<Integer> stock = new HashSet<>(Arrays.asList(10, 20, 30, 40, 50));
        List<Integer> soldOut = Arrays.asList(20, 40, 40, 99);

        System.out.println("first call: " + stock.removeAll(soldOut));
        System.out.println("second call: " + stock.removeAll(soldOut));
        System.out.println("left: " + new TreeSet<>(stock));
    }
}

How It Works

  1. A list is accepted. The parameter is a Collection, so soldOut can stay a plain List.
  2. 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.
  3. 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.
  4. 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));
    }
}

How It Works

  1. a minus b. 3 and 4 are in both sets, so removing b's elements from a copy of a leaves 1 and 2.
  2. b minus a. The mirror-image call leaves 5 and 6, which proves that swapping the two sets changes the answer.
  3. Combine the leftovers. addAll merges the two differences into the elements that belong to exactly one of the sets.
  4. 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.
Yes. removeAll(null) throws NullPointerException.

Next: removeIf()

Remove every element that passes a test (Java 8).

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

9 people found this page helpful