Home Java ArrayDeque Methods clear() Java ArrayDeque clear() Method Definition In Java, clear() removes all elements from an ArrayDeque. Afterwards size() is 0 and isEmpty() is true. The method returns void.
The deque object itself stays the same, so every variable that refers to it sees the empty deque, and you can keep adding to it. This differs from assigning new ArrayDeque<>(), which only changes one variable.
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 .
Syntax Call clear with no arguments.
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b"));
deque.clear(); // deque = []
int n = deque.size(); // 0 Call What it does deque.clear()Removes every element. deque = new ArrayDeque<>()Points this variable to a new, empty deque; others still see the old one. deque.removeIf(x -> true)Also removes everything, but less clearly. deque.retainAll(emptyList)Same result again; prefer clear().
clear() also sets the internal array slots to null so that the removed elements can be garbage-collected. The time it takes grows with the number of elements removed.
Library declarations // java.util.ArrayDeque<E>
public void clear()
// Declared in Collection<E> Parameters Parameter Description None clear() takes no parameters.
Return Value Return Value Description voidNothing is returned. The deque is empty.
Important
After clear(), the throwing methods (pop, remove, getFirst, ...) throw NoSuchElementException and the special-value methods (poll, peek, ...) return null.
Compare with Similar Methods Ways to end up with an empty deque:
Method How it differs poll() loopRemoves one element at a time so you can process each. removeAll(c)Removes only elements found in another collection. retainAll(c)With an empty collection, also removes everything. new ArrayDeque<>()A different object; other variables still point to the old, full deque.
Java Examples 1. Empty a cart 2. Reuse the deque 3. Shared references Example 1 — Clear a Shopping Cart Remove all items with one call and check the size.
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeClearDemo {
public static void main(String[] args) {
Deque<String> cart = new ArrayDeque<>(Arrays.asList("pen", "book", "lamp"));
System.out.println("Before: " + cart + " size=" + cart.size());
cart.clear();
System.out.println("After: " + cart + " size=" + cart.size());
System.out.println("Empty? " + cart.isEmpty());
}
} Before: [pen, book, lamp] size=3
After: [] size=0
Empty? true How It Works Three items. The cart starts with a pen, a book, and a lamp.One call. cart.clear() removes all three at once.Empty brackets. The deque now prints as [] with size 0.Confirmed. isEmpty() returns true.Example 2 — Reuse One Deque for Several Rounds Fill the same deque, print it, and clear it at the end of every round.
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeClearReuse {
public static void main(String[] args) {
Deque<Integer> buffer = new ArrayDeque<>();
for (int round = 1; round <= 2; round++) {
buffer.add(round * 10);
buffer.add(round * 10 + 1);
System.out.println("Round " + round + ": " + buffer);
buffer.clear();
}
System.out.println("End: " + buffer);
}
} Round 1: [10, 11]
Round 2: [20, 21]
End: [] How It Works One deque, created once. buffer is made before the loop.Round 1. 10 and 11 are added, printed, then cleared.Round 2. The same deque starts empty again, so it shows only 20 and 21.Final state. The last clear() leaves it empty: [].Example 3 — clear() vs Assigning a New Deque Two variables point to one deque. Reassigning one variable does not empty the deque; clear() does.
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeClearShared {
public static void main(String[] args) {
Deque<String> shared = new ArrayDeque<>(Arrays.asList("a", "b"));
Deque<String> alias = shared;
alias = new ArrayDeque<>();
System.out.println("after reassign: " + shared);
alias = shared;
alias.clear();
System.out.println("after clear: " + shared);
System.out.println("poll on empty: " + shared.poll());
}
} after reassign: [a, b]
after clear: []
poll on empty: null How It Works Two names, one deque. alias = shared copies the reference, not the elements.Reassigning. alias = new ArrayDeque<>() points alias at a new empty deque; shared still holds [a, b].Clearing. After pointing alias back at the shared deque, alias.clear() empties the one object both variables use.Empty behavior. poll() on the cleared deque returns null.Frequently Asked Questions What does ArrayDeque clear() do? It removes all elements, leaving an empty deque.
Can I keep using a deque after clear()? Yes. It is the same object, now empty, and you can add new elements to it.
What is the difference between clear() and new ArrayDeque<>()? clear() empties the existing object, so all references see it empty. Assigning a new deque only changes the one variable.
What does poll() return after clear()? null, because the deque is empty. pop() and removeFirst() would throw NoSuchElementException.
Does clear() return anything? No. Its return type is void.
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.
11 people found this page helpful
Helpful Share Copy link Suggestion