Home Java LinkedList Methods retainAll() Java LinkedList retainAll() Method Definition In Java, retainAll(c) keeps in the LinkedList only the elements that are also found in the collection c, and removes all the others. This is the intersection of the two groups.
The kept elements stay in their original order, and duplicates of kept values stay too. The collection c itself is not changed.
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 keep.
LinkedList<String> list = new LinkedList<>(Arrays.asList("a", "b", "c", "b"));
boolean changed = list.retainAll(Arrays.asList("b", "c", "z")); // true
// list is now [b, c, b] Call What it does list.retainAll(c)Keeps only the elements that are in c. list.removeAll(c)Removes the elements that are in c. list.contains(o)Checks one value at a time. list.removeIf(filter)Removes elements by a rule.
The result is true if the list changed, so keeping everything returns false.
Library declarations // java.util.LinkedList (inherited from AbstractCollection)
public boolean retainAll(Collection<?> c) Parameters Parameter Description c (Collection<?>)The values to keep. It must not be null.
Return Value Return Value Description booleantrue if at least one element was removed.
Important
Passing an empty collection removes every element, because nothing in the list is "also in" an empty group. Passing null throws NullPointerException. The list does not become a set: if a kept value appears twice in the list, both copies stay.
Compare with Similar Methods Bulk removal and filtering:
Java Examples 1. Common elements 2. Empty argument 3. Duplicates stay Example 1 — Find the Items Two Lists Share Keep only the guests that are on both lists.
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class LinkedListRetainAllDemo {
public static void main(String[] args) {
LinkedList<String> invited = new LinkedList<>(
Arrays.asList("Ana", "Ben", "Cleo", "Dev"));
List<String> confirmed = Arrays.asList("Dev", "Ana", "Eve");
boolean changed = invited.retainAll(confirmed);
System.out.println("changed: " + changed);
System.out.println("coming: " + invited);
}
} changed: true
coming: [Ana, Dev] How It Works Two guest lists. Four people were invited, and three people confirmed.Ben and Cleo are removed. They are not on the confirmed list.Eve is ignored. She confirmed but was never invited, so she does not appear in the invited list.Original order is kept. Ana comes before Dev, as in the invited list, even though the confirmed list has Dev first.Example 2 — See What an Empty Collection Does Keep everything, then keep nothing, and compare the results.
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListRetainAllEmpty {
public static void main(String[] args) {
LinkedList<Integer> a = new LinkedList<>(Arrays.asList(1, 2, 3));
boolean keepAll = a.retainAll(Arrays.asList(1, 2, 3, 4));
System.out.println("keep all: " + keepAll + " " + a);
boolean keepNone = a.retainAll(new LinkedList<Integer>());
System.out.println("keep none: " + keepNone + " " + a);
System.out.println("is empty: " + a.isEmpty());
}
} keep all: false [1, 2, 3]
keep none: true []
is empty: true How It Works Every element is on the keep list. 1, 2 and 3 are all in the argument, so nothing is removed.The result is false. The list did not change.An empty keep list. No value is "also in" an empty collection, so every element is removed.The result is true. The list changed, and it ends up empty.Example 3 — Keep Duplicates of Kept Values Use retainAll on a list with repeated values and see that repeats survive.
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class LinkedListRetainAllDups {
public static void main(String[] args) {
LinkedList<String> cart = new LinkedList<>(
Arrays.asList("pen", "ink", "pen", "pad", "ink", "pen"));
List<String> inStock = Arrays.asList("pen", "pad");
cart.retainAll(inStock);
System.out.println("cart: " + cart);
System.out.println("pens: " + cart.indexOf("pen") + " first, "
+ cart.lastIndexOf("pen") + " last");
}
} cart: [pen, pen, pad, pen]
pens: 0 first, 3 last How It Works A cart with repeats. There are three pens, two inks and one pad.ink is dropped. It is not in the in-stock list, so both inks go.pen and pad stay. Every one of the three pens is kept, because each one is found in the argument.Positions after the cleanup. The list is pen, pen, pad, pen, so the first pen is at index 0 and the last pen at index 3.Frequently Asked Questions What does LinkedList retainAll() do? It keeps only the elements that are also in the given collection and removes all the others.
How do I get the intersection of two lists in Java? Copy the first list, then call copy.retainAll(second). The copy now holds the common elements.
What happens if I pass an empty collection to retainAll()? Every element is removed, and the list becomes empty.
What does retainAll() return? true if the list changed, and false if every element was already in the collection.
Does retainAll() remove duplicates? No. If a value is kept, all its copies are kept. It only removes values that are not in the argument.
Next: set() Replace the element at an index and return the old one.
set() tutorial → 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.
8 people found this page helpful
Helpful Share Copy link Suggestion