Home Java ArrayDeque Methods retainAll() Java ArrayDeque retainAll() Method 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.
Deque<String> deque = new ArrayDeque<>(Arrays.asList("red", "green", "blue"));
boolean changed = deque.retainAll(Arrays.asList("green", "blue")); // true, [green, blue] Call What 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.util.ArrayDeque<E>
public boolean retainAll(Collection<?> c)
// Declared in Collection<E> Parameters Parameter Description cRequired. The collection of values to keep. Must not be null.
Return Value Return Value Description booleantrue if any element was removed; false if every element was already in c.
Compare with Similar Methods Filtering methods side by side:
Method How 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 1. Keep some colors 2. false and empty 3. Common names Example 1 — Keep Only Certain Values Keep green and blue, including both copies of green.
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);
}
} true
[green, blue, green] How It Works Keep list. Only green and blue may stay.red is dropped. It is not in the keep list, so it is removed, and the method returns true.Duplicates survive. Both green elements are kept, because each one is contained in the keep list.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.
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 All allowed. Both 2 and 4 are in [1, 2, 3, 4], so nothing is removed and the result is false.Extra values do not matter. 1 and 3 in the argument are simply ignored; retainAll never adds anything.Empty keep list. No element is contained in an empty list, so both are removed and the result is true.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.
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);
}
} both days: [Ana, Dev]
monday untouched: [Ana, Ben, Cy, Dev] How It Works Copy first. both starts as a separate copy of monday.Intersection. Only Ana and Dev are in tuesday, so Ben and Cy are removed from the copy.Monday's order wins. The result is [Ana, Dev], following the copy's order, not Tuesday's.Original safe. monday still has all four names because only the copy was changed.Frequently Asked Questions What does ArrayDeque retainAll() do? It keeps only the elements that are also in the given collection and removes all the others.
What does retainAll() return? true if any element was removed, false if the deque already contained only allowed elements.
How do I get the intersection of two deques without changing them? Copy one with new ArrayDeque<>(first) and call retainAll(second) on the copy.
What happens if I pass an empty collection? Every element is removed, because nothing is contained in an empty collection.
What happens with retainAll(null)? It throws NullPointerException.
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.
12 people found this page helpful
Helpful Share Copy link Suggestion