Java LinkedList retainAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

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.

Java
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]
CallWhat 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
// java.util.LinkedList (inherited from AbstractCollection)
public boolean retainAll(Collection<?> c)

Parameters

ParameterDescription
c (Collection<?>)The values to keep. 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
removeAll(c)The opposite: removes the elements that are in another collection.
contains(o)Tests a single value instead of a whole collection.
removeIf(filter)Removes elements by a rule instead of by membership.
HashSet.retainAll()The same operation on a set.

Java Examples

Example 1 — Find the Items Two Lists Share

Keep only the guests that are on both lists.

Java
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);
    }
}

How It Works

  1. Two guest lists. Four people were invited, and three people confirmed.
  2. Ben and Cleo are removed. They are not on the confirmed list.
  3. Eve is ignored. She confirmed but was never invited, so she does not appear in the invited list.
  4. 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.

Java
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());
    }
}

How It Works

  1. Every element is on the keep list. 1, 2 and 3 are all in the argument, so nothing is removed.
  2. The result is false. The list did not change.
  3. An empty keep list. No value is "also in" an empty collection, so every element is removed.
  4. 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.

Java
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");
    }
}

How It Works

  1. A cart with repeats. There are three pens, two inks and one pad.
  2. ink is dropped. It is not in the in-stock list, so both inks go.
  3. pen and pad stay. Every one of the three pens is kept, because each one is found in the argument.
  4. 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

It keeps only the elements that are also in the given collection and removes all the others.
Copy the first list, then call copy.retainAll(second). The copy now holds the common elements.
Every element is removed, and the list becomes empty.
true if the list changed, and false if every element was already in the collection.
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

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.

8 people found this page helpful