Java ArrayList retainAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, retainAll(c) keeps only the elements of an ArrayList that are also contained in the collection c and removes all others.

It is the list version of an intersection. Duplicates in the list are kept as long as their value is in c, and the kept elements stay in their original order. It is the opposite of removeAll(c).

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 keep.

Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "a", "c"));
list.retainAll(Arrays.asList("a", "c"));   // true, list = [a, a, c]
list.retainAll(Arrays.asList("a", "c"));   // false, nothing more to remove
CallWhat it does
list.retainAll(c)Keeps only the elements that c contains.
list.retainAll(Collections.emptyList())Removes everything (clear() is clearer).
new ArrayList<>(a) then retainAll(b)An intersection that leaves a unchanged.
list.removeAll(c)The opposite: removes the elements that c contains.

Like removeAll, it calls c.contains() once per element, so use a HashSet for c when it is large.

Library declarations
Java
// java.util.ArrayList<E>
public boolean retainAll(Collection<?> c)

// Declared in Collection<E> and List<E>

Parameters

ParameterDescription
c (Collection<?>)The values to keep. Must not be null.

Return Value

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

Compare with Similar Methods

Filtering a list:

MethodHow it differs
removeAll(c)Removes the elements in c instead of keeping them.
removeIf(filter)Decides with a condition instead of a collection.
contains(o)Checks a single value without changing the list.
clear()Same as retaining against an empty collection, but clearer.

Java Examples

Example 1 — Find Common Friends

Copy one list and keep only the names that the other list also has.

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

public class ArrayListRetainAllDemo {
    public static void main(String[] args) {
        List<String> mine = Arrays.asList("Ali", "Bea", "Cal", "Dee");
        List<String> yours = Arrays.asList("Dee", "Eli", "Ali");

        List<String> common = new ArrayList<>(mine);
        common.retainAll(yours);

        System.out.println("common: " + common);
        System.out.println("mine unchanged: " + mine);
    }
}

How It Works

  1. Copy first. common starts as a copy of mine, so the original list is not touched.
  2. Keep matches. retainAll removes Bea and Cal, because yours does not contain them.
  3. The first list's order. The result follows mine: Ali, then Dee, even though yours lists Dee first.
  4. Original safe. mine still prints all four names.

Example 2 — Duplicates and the Return Value

Call retainAll twice with the same argument and compare the results.

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

public class ArrayListRetainAllReturn {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 2, 3, 2));
        List<Integer> keep = Arrays.asList(2, 3);

        System.out.println("first call:  " + nums.retainAll(keep) + " " + nums);
        System.out.println("second call: " + nums.retainAll(keep) + " " + nums);
    }
}

How It Works

  1. Only 1 goes. keep contains 2 and 3, so the single 1 is removed.
  2. Copies kept. All three 2s stay, because each one is contained in keep.
  3. First result true. Something was removed, so the first call reports a change.
  4. Second result false. Every element already matches, nothing is removed, and retainAll returns false.

Example 3 — Keep Only Allowed Tags (with a HashSet)

Filter a tag list against a set of allowed tags, then retain against an empty set.

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

public class ArrayListRetainAllAllowed {
    public static void main(String[] args) {
        Set<String> allowed = new HashSet<>(Arrays.asList("java", "sql", "git"));
        List<String> tags = new ArrayList<>(Arrays.asList("java", "fun", "git", "java", "misc"));

        tags.retainAll(allowed);
        System.out.println(tags);

        tags.retainAll(new HashSet<String>());
        System.out.println(tags + " empty=" + tags.isEmpty());
    }
}

How It Works

  1. A set for lookups. allowed is a HashSet, so each contains check inside retainAll is quick.
  2. Filter the tags. fun and misc are not allowed and are removed; java appears twice and both copies stay.
  3. Order from the list. The kept tags appear in the list's order, not in the set's order.
  4. Empty set. Retaining against an empty set removes everything, the same effect as clear().

Frequently Asked Questions

It keeps only the elements that are also in the collection you pass and removes all others.
Copy one first: List<String> common = new ArrayList<>(a); common.retainAll(b);
true if any element was removed, false if the list was left unchanged.
No. If a value is in the collection, every copy of it in the list is kept.
Nothing matches, so every element is removed and the list becomes empty.

Next: set()

Replace the element at an index.

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.

12 people found this page helpful