Java ArrayDeque removeAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, removeAll(c) removes every element of the ArrayDeque that is also contained in the collection c. All copies are removed, not just the first. The remaining elements keep their order.

It returns true if at least one element was removed. The opposite operation is retainAll(c), which keeps only the elements that are in c.

An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety. removeAll comes from the Collection interface and works on every Java version; newer JDKs override it in ArrayDeque with a faster single pass, with the same result.

Syntax

Pass a collection of values to remove.

Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b", "c", "b"));
boolean changed = deque.removeAll(Arrays.asList("b"));   // true, [a, c]
CallWhat it does
deque.removeAll(list)Removes every element found in list.
deque.removeAll(set)Same, but a HashSet argument makes each lookup fast.
deque.removeAll(emptyList)Removes nothing, returns false.
deque.removeAll(null)Throws NullPointerException.

For each deque element, removeAll asks c.contains(element). With a List argument that is a linear search each time; for large inputs, pass a HashSet.

Library declarations
Java
// java.util.ArrayDeque<E>
public boolean removeAll(Collection<?> c)

// Declared in Collection<E>

Parameters

ParameterDescription
cRequired. The collection of values to remove. Must not be null. Its order does not matter.

Return Value

Return ValueDescription
booleantrue if the deque changed; false if no element matched.

Compare with Similar Methods

Bulk and conditional removal compared:

MethodHow it differs
retainAll(c)Keeps elements in c and removes the rest: the opposite filter.
removeIf(filter)Removes by a condition instead of by another collection.
remove(o)Removes only the first copy of one value.
clear()Removes everything, no matching needed.

Java Examples

Example 1 — Remove Several Values at Once

Remove every b and d from a deque.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeRemoveAllDemo {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>(Arrays.asList("a", "b", "c", "b", "d"));

        boolean changed = d.removeAll(Arrays.asList("b", "d"));

        System.out.println(changed);
        System.out.println(d);
    }
}

How It Works

  1. Values to remove. The argument list holds b and d.
  2. Every copy goes. Both b elements are removed, not just the first one, and d is removed too.
  3. Order of survivors. a and c stay in their original order.
  4. Changed. Three elements were removed, so the method returns true.

Example 2 — When removeAll Returns false

No element matches, and an empty argument removes nothing either.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;

public class ArrayDequeRemoveAllNothing {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(Arrays.asList(1, 2, 3));

        System.out.println(d.removeAll(Arrays.asList(7, 8)));
        System.out.println(d);
        System.out.println(d.removeAll(Collections.emptyList()));
    }
}

How It Works

  1. No overlap. None of 1, 2, 3 is in [7, 8], so nothing is removed and the result is false.
  2. Deque intact. It still prints [1, 2, 3].
  3. Empty argument. Collections.emptyList() contains nothing, so again nothing matches.
  4. Wildcard parameter. The parameter type is Collection<?>, so a list of any element type is accepted.

Example 3 — Filter Out Stop Words with a Set

Remove common words using a HashSet as the argument.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashSet;
import java.util.Set;

public class ArrayDequeStopWords {
    public static void main(String[] args) {
        Deque<String> words = new ArrayDeque<>(
            Arrays.asList("the", "cat", "sat", "on", "the", "mat"));
        Set<String> stop = new HashSet<>(Arrays.asList("the", "on"));

        words.removeAll(stop);

        System.out.println(words);
        System.out.println(stop.size() + " stop words");
    }
}

How It Works

  1. A sentence as a deque. Six words, with the appearing twice.
  2. A set to check against. A HashSet answers contains quickly, which matters when the deque is long.
  3. Set order does not matter. The result keeps the deque's own order: [cat, sat, mat].
  4. Argument untouched. The set still has its 2 words after the call.

Frequently Asked Questions

It removes every element of the deque that is also in the given collection, and returns true if anything was removed.
Yes. Every element that the argument collection contains is removed, including duplicates.
removeAll removes the elements that are in the argument; retainAll keeps only those elements and removes the rest.
It throws NullPointerException.
Pass a HashSet instead of a List, so each contains check is fast.

Next: removeFirst()

Remove and return the head; throws if empty.

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

15 people found this page helpful