Home Java LinkedList Methods removeAll() Java LinkedList removeAll() Method 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.
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] Call What 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.util.LinkedList (inherited from AbstractCollection)
public boolean removeAll(Collection<?> c) Parameters Parameter Description c (Collection<?>)The values to remove. It must not be null.
Return Value Return Value Description booleantrue if at least one element was removed.
Important
A null argument throws NullPointerException. Each element of the list is looked up with c.contains(...), so if c is a long ArrayList, the work can be large. Wrapping the values in a HashSet first makes the lookup fast.
Compare with Similar Methods Bulk removal and filtering:
Method How 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 1. Subtract a list 2. No match and null 3. Blocklist with a set Example 1 — Remove Several Values at Once Remove all the unwanted topics from a list in one call.
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);
}
} changed: true
topics: [java, html]
unwanted: [css, sql, php] How It Works A list with a duplicate. "css" appears twice in the topics.Every match is removed. Both "css" entries and the "sql" entry are deleted in one call."php" is ignored. It is in the removal list but not in topics, which is fine.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.
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");
}
}
} changed: false, list: [1, 2, 3]
empty argument: false
removeAll(null) -> NullPointerException How It Works No overlap. The numbers 8 and 9 are not in the list, so no element matches.The result is false. Nothing was removed, so the list still holds 1, 2 and 3.An empty argument. Removing "nothing" also changes nothing, and returns false.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.
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());
}
} kept: [cat, dog, bird]
count: 3 How It Works A sentence as a list. It has eight words, with "the" and "and" repeated.A set of stop words. The set holds "the", "and" and "a", and checks membership quickly.All copies disappear. Both "the" entries, both "and" entries and the "a" entry are removed.Three words remain. "cat", "dog" and "bird" keep their original order.Frequently Asked Questions What does LinkedList removeAll() do? It removes from the list every element that is also contained in the given collection.
Does removeAll() remove duplicates too? 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.
What does removeAll() return? true if the list changed, and false if no element was removed.
What happens if I pass null to removeAll()? It throws a NullPointerException.
What is the difference between removeAll() and retainAll()? removeAll deletes the elements that are in the other collection. retainAll keeps only those elements and deletes the rest.
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.
10 people found this page helpful
Helpful Share Copy link Suggestion