Java LinkedList removeAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, removeAll(c) removes from the LinkedList every element that is also found in the collection c. It is like subtracting one group from another.

Every copy of a matching value is removed, not just the first. The other elements keep their order, and the collection c itself is left unchanged.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Collection, and LinkedList implements it.

Syntax

Pass the collection of values to remove.

Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("a", "b", "c", "b"));
boolean changed = list.removeAll(Arrays.asList("b", "z"));  // true
// list is now [a, c]
CallWhat it does
list.removeAll(c)Removes every element that is in c.
list.retainAll(c)Keeps only the elements that are in c.
list.removeIf(filter)Removes elements that pass a test.
list.remove(o)Removes only the first match of one value.

The result is true if the list changed, and false if nothing was removed.

Library declarations
Java
// java.util.LinkedList (inherited from AbstractCollection)
public boolean removeAll(Collection<?> c)

Parameters

ParameterDescription
c (Collection<?>)The values to remove. It must not be null.

Return Value

Return ValueDescription
booleantrue if at least one element was removed.

Compare with Similar Methods

Bulk removal and filtering:

MethodHow it differs
retainAll(c)The opposite: keeps only the elements that are in another collection.
removeIf(filter)Removes by a rule instead of by a collection of values.
clear()Removes everything without a collection.
HashSet.removeAll()The same operation on a set, where there are no duplicates.

Java Examples

Example 1 — Remove Several Values at Once

Remove all the unwanted topics from a list in one call.

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

public class LinkedListRemoveAllDemo {
    public static void main(String[] args) {
        LinkedList<String> topics = new LinkedList<>(
            Arrays.asList("java", "css", "sql", "css", "html"));
        List<String> unwanted = Arrays.asList("css", "sql", "php");

        boolean changed = topics.removeAll(unwanted);
        System.out.println("changed: " + changed);
        System.out.println("topics: " + topics);
        System.out.println("unwanted: " + unwanted);
    }
}

How It Works

  1. A list with a duplicate. "css" appears twice in the topics.
  2. Every match is removed. Both "css" entries and the "sql" entry are deleted in one call.
  3. "php" is ignored. It is in the removal list but not in topics, which is fine.
  4. The argument is untouched. The unwanted list still holds its three values, and changed is true.

Example 2 — See When removeAll Changes Nothing

Remove values that are not in the list, then pass null to see the exception.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListRemoveAllNothing {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1, 2, 3));

        boolean changed = list.removeAll(Arrays.asList(8, 9));
        System.out.println("changed: " + changed + ", list: " + list);

        boolean emptyArg = list.removeAll(new LinkedList<Integer>());
        System.out.println("empty argument: " + emptyArg);

        try {
            list.removeAll(null);
        } catch (NullPointerException e) {
            System.out.println("removeAll(null) -> NullPointerException");
        }
    }
}

How It Works

  1. No overlap. The numbers 8 and 9 are not in the list, so no element matches.
  2. The result is false. Nothing was removed, so the list still holds 1, 2 and 3.
  3. An empty argument. Removing "nothing" also changes nothing, and returns false.
  4. null is rejected. A null collection throws NullPointerException, instead of being treated as empty.

Example 3 — Filter a List with a HashSet Blocklist

Use a HashSet for the values to remove, so each lookup is fast.

Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

public class LinkedListRemoveAllSet {
    public static void main(String[] args) {
        LinkedList<String> words = new LinkedList<>(
            Arrays.asList("the", "cat", "and", "the", "dog", "and", "a", "bird"));
        Set<String> stopWords = new HashSet<>(Arrays.asList("the", "and", "a"));

        words.removeAll(stopWords);

        System.out.println("kept: " + words);
        System.out.println("count: " + words.size());
    }
}

How It Works

  1. A sentence as a list. It has eight words, with "the" and "and" repeated.
  2. A set of stop words. The set holds "the", "and" and "a", and checks membership quickly.
  3. All copies disappear. Both "the" entries, both "and" entries and the "a" entry are removed.
  4. Three words remain. "cat", "dog" and "bird" keep their original order.

Frequently Asked Questions

It removes from the list every element that is also contained in the given collection.
It removes every copy of each matching value, not just the first. It does not remove duplicates of values that are not in the argument.
true if the list changed, and false if no element was removed.
It throws a NullPointerException.
removeAll deletes the elements that are in the other collection. retainAll keeps only those elements and deletes the rest.

Next: removeFirst()

Remove and return the first element, or throw 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.

10 people found this page helpful