Java ArrayList removeAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, removeAll(c) removes from an ArrayList every 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
CallWhat 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

ParameterDescription
c (Collection<?>)The values to remove. It may hold any type. Must not be null.

Return Value

Return ValueDescription
booleantrue if at least one element was removed; otherwise false.

Compare with Similar Methods

Ways to remove several elements:

MethodHow it differs
remove(o)Removes only the first element equal to o.
retainAll(c)Keeps the elements in c and removes the rest.
removeIf(filter)Removes elements that match a condition instead of a collection.
clear()Removes every element.

Java Examples

Example 1 — Remove Sold-Out Dishes

Remove every dish that appears in a sold-out list.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListRemoveAllDemo {
    public static void main(String[] args) {
        List<String> menu = new ArrayList<>(Arrays.asList("soup", "salad", "pasta", "pie"));
        List<String> soldOut = Arrays.asList("pasta", "soup", "steak");

        boolean changed = menu.removeAll(soldOut);
        System.out.println(changed + " " + menu);
    }
}

How It Works

  1. Menu and sold-out list. The second list names the dishes that are no longer available.
  2. Every match removed. soup and pasta are in soldOut, so both disappear from the menu.
  3. Unknown items ignored. steak is not on the menu; it is simply never matched and causes no error.
  4. Order kept. The surviving dishes stay in their original order, and true reports that the menu changed.

Example 2 — removeAll() Deletes Every Copy

Compare remove and removeAll on a list with the value 6 three times.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayListRemoveAllCopies {
    public static void main(String[] args) {
        List<Integer> rolls = new ArrayList<>(Arrays.asList(6, 2, 6, 3, 6));
        List<Integer> copy = new ArrayList<>(rolls);

        copy.remove(Integer.valueOf(6));
        System.out.println("remove(6):    " + copy);

        rolls.removeAll(Collections.singleton(6));
        System.out.println("removeAll(6): " + rolls);
    }
}

How It Works

  1. Three sixes. The dice rolls contain the value 6 three times.
  2. remove takes one. remove(Integer.valueOf(6)) on the copy deletes only the first 6.
  3. removeAll takes all. Collections.singleton(6) is a one-element set, and removeAll deletes every element equal to it.
  4. Two lists, two results. The copy still has two sixes; the original has none.

Example 3 — Nothing to Remove, and a null Argument

Try a collection with no overlap, an empty collection, and null.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListRemoveAllEdges {
    public static void main(String[] args) {
        List<String> files = new ArrayList<>(Arrays.asList("a.txt", "b.txt"));

        System.out.println("no overlap: " + files.removeAll(Arrays.asList("c.txt")));
        System.out.println("empty arg:  " + files.removeAll(new ArrayList<String>()));
        System.out.println("still " + files);

        try {
            files.removeAll(null);
        } catch (NullPointerException e) {
            System.out.println("removeAll(null): " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. No match. c.txt is not in the list, so removeAll returns false.
  2. Empty collection. There is nothing to compare against, so again nothing is removed and the result is false.
  3. List intact. Both calls left the two file names in place.
  4. null is rejected. Passing null throws NullPointerException; pass an empty collection when you mean "remove nothing".

Frequently Asked Questions

It removes every element of the list that is also contained in the collection you pass.
Yes. Every element equal to a value in the collection is removed, however many times it appears.
true if the list changed, false if nothing was removed.
remove(o) removes only the first element equal to o. removeAll(c) removes all elements that appear in c.
It calls contains() on the argument once per element, and List.contains scans the list. Wrap the argument in a HashSet to make each check fast.

Next: removeFirst()

Remove and return the first element (Java 21+).

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.

7 people found this page helpful