Home Java ArrayDeque Methods removeAll() Java ArrayDeque removeAll() Method 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.
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b", "c", "b"));
boolean changed = deque.removeAll(Arrays.asList("b")); // true, [a, c] Call What 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.util.ArrayDeque<E>
public boolean removeAll(Collection<?> c)
// Declared in Collection<E> Parameters Parameter Description cRequired. The collection of values to remove. Must not be null. Its order does not matter.
Return Value Return Value Description booleantrue if the deque changed; false if no element matched.
Compare with Similar Methods Bulk and conditional removal compared:
Method How 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 1. Remove several values 2. Nothing removed 3. Remove stop words Example 1 — Remove Several Values at Once Remove every b and d from a deque.
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 Values to remove. The argument list holds b and d.Every copy goes. Both b elements are removed, not just the first one, and d is removed too.Order of survivors. a and c stay in their original order.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.
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 No overlap. None of 1, 2, 3 is in [7, 8], so nothing is removed and the result is false.Deque intact. It still prints [1, 2, 3].Empty argument. Collections.emptyList() contains nothing, so again nothing matches.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.
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");
}
} [cat, sat, mat]
2 stop words How It Works A sentence as a deque. Six words, with the appearing twice.A set to check against. A HashSet answers contains quickly, which matters when the deque is long.Set order does not matter. The result keeps the deque's own order: [cat, sat, mat].Argument untouched. The set still has its 2 words after the call.Frequently Asked Questions What does ArrayDeque removeAll() do? It removes every element of the deque that is also in the given collection, and returns true if anything was removed.
Does removeAll() remove all copies of a value? Yes. Every element that the argument collection contains is removed, including duplicates.
What is the difference between removeAll() and retainAll()? removeAll removes the elements that are in the argument; retainAll keeps only those elements and removes the rest.
What happens with removeAll(null)? It throws NullPointerException.
How can I make removeAll() faster? Pass a HashSet instead of a List, so each contains check is fast.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion