In Java, removeAll(c) removes from an ArrayListevery element that is also contained in the collection c. All copies of a matching value are removed, not just the first.
It works like a set difference: the list minus c. The remaining elements keep their order. Membership is checked with c.contains(), which uses equals().
An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.
Syntax
Pass the collection of values to remove.
Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "a", "c"));
list.removeAll(Arrays.asList("a", "z")); // true, list = [b, c]
list.removeAll(Arrays.asList("q")); // false, unchanged
Call
What it does
list.removeAll(c)
Removes every element that c contains.
list.removeAll(Collections.singleton(x))
Removes all copies of one value.
list.removeAll(new HashSet<>(big))
Faster when big is large, because HashSet.contains is quick.
list.retainAll(c)
The opposite: keeps only elements that c contains.
For each element of the list, removeAll asks c.contains(element). If c is a big List, each of those checks is a slow scan; turning c into a HashSet first makes every check fast.
Library declarations
Java
// java.util.ArrayList<E>
public boolean removeAll(Collection<?> c)
// Declared in Collection<E> and List<E>
Parameters
Parameter
Description
c (Collection<?>)
The values to remove. It may hold any type. Must not be null.
Return Value
Return Value
Description
boolean
true if at least one element was removed; otherwise false.