Java ArrayDeque retainAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, retainAll(c) keeps only the elements of the ArrayDeque that are also contained in the collection c, and removes all the others. Duplicates of a kept value are all kept. The survivors keep their original order.

It returns true if the deque changed. It is the opposite of removeAll(c), and a simple way to compute an intersection of two collections.

An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety. retainAll comes from the Collection interface and works on every Java version; newer JDKs override it in ArrayDeque with a faster single pass, with the same result.

Syntax

Pass the collection of values to keep.

Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("red", "green", "blue"));
boolean changed = deque.retainAll(Arrays.asList("green", "blue"));   // true, [green, blue]
CallWhat it does
deque.retainAll(list)Keeps elements found in list.
deque.retainAll(emptyList)Removes everything (if the deque was not empty).
copy.retainAll(other)Intersection without changing the original, if copy is a copy.
deque.retainAll(null)Throws NullPointerException.

retainAll changes the deque in place. To keep the original, make a copy first with new ArrayDeque<>(original) and call retainAll on the copy.

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

// Declared in Collection<E>

Parameters

ParameterDescription
cRequired. The collection of values to keep. Must not be null.

Return Value

Return ValueDescription
booleantrue if any element was removed; false if every element was already in c.

Compare with Similar Methods

Filtering methods side by side:

MethodHow it differs
removeAll(c)Removes elements that are in c; retainAll keeps them.
removeIf(filter)Uses a condition. retainAll(c) is like removeIf(e -> !c.contains(e)).
contains(o)Asks about one value without removing anything.
clear()Same result as retainAll with an empty collection.

Java Examples

Example 1 — Keep Only Certain Values

Keep green and blue, including both copies of green.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeRetainAllDemo {
    public static void main(String[] args) {
        Deque<String> colors = new ArrayDeque<>(
            Arrays.asList("red", "green", "blue", "green"));

        boolean changed = colors.retainAll(Arrays.asList("green", "blue"));

        System.out.println(changed);
        System.out.println(colors);
    }
}

How It Works

  1. Keep list. Only green and blue may stay.
  2. red is dropped. It is not in the keep list, so it is removed, and the method returns true.
  3. Duplicates survive. Both green elements are kept, because each one is contained in the keep list.
  4. Original order. The survivors stay in deque order: [green, blue, green].

Example 2 — No Change, Then Keep Nothing

When every element is already allowed, nothing changes. An empty keep list removes everything.

Java
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeRetainAllEdges {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(Arrays.asList(2, 4));

        System.out.println(d.retainAll(Arrays.asList(1, 2, 3, 4)));
        System.out.println(d);

        System.out.println(d.retainAll(new ArrayList<Integer>()));
        System.out.println(d);
    }
}

How It Works

  1. All allowed. Both 2 and 4 are in [1, 2, 3, 4], so nothing is removed and the result is false.
  2. Extra values do not matter. 1 and 3 in the argument are simply ignored; retainAll never adds anything.
  3. Empty keep list. No element is contained in an empty list, so both are removed and the result is true.
  4. Empty deque. The last line prints [].

Example 3 — Who Came on Both Days?

Copy Monday's list and keep only names that also appear on Tuesday.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeBothDays {
    public static void main(String[] args) {
        Deque<String> monday = new ArrayDeque<>(Arrays.asList("Ana", "Ben", "Cy", "Dev"));
        Deque<String> tuesday = new ArrayDeque<>(Arrays.asList("Dev", "Ana", "Eli"));

        Deque<String> both = new ArrayDeque<>(monday);
        both.retainAll(tuesday);

        System.out.println("both days: " + both);
        System.out.println("monday untouched: " + monday);
    }
}

How It Works

  1. Copy first. both starts as a separate copy of monday.
  2. Intersection. Only Ana and Dev are in tuesday, so Ben and Cy are removed from the copy.
  3. Monday's order wins. The result is [Ana, Dev], following the copy's order, not Tuesday's.
  4. Original safe. monday still has all four names because only the copy was changed.

Frequently Asked Questions

It keeps only the elements that are also in the given collection and removes all the others.
true if any element was removed, false if the deque already contained only allowed elements.
Copy one with new ArrayDeque<>(first) and call retainAll(second) on the copy.
Every element is removed, because nothing is contained in an empty collection.
It throws NullPointerException.

Next: size()

Count how many elements the deque holds.

size() 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