Java ArrayDeque clear() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

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.

Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b"));
deque.clear();              // deque = []
int n = deque.size();       // 0
CallWhat 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
// java.util.ArrayDeque<E>
public void clear()

// Declared in Collection<E>

Parameters

ParameterDescription
Noneclear() takes no parameters.

Return Value

Return ValueDescription
voidNothing is returned. The deque is empty.

Compare with Similar Methods

Ways to end up with an empty deque:

MethodHow 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

Example 1 — Clear a Shopping Cart

Remove all items with one call and check the size.

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

How It Works

  1. Three items. The cart starts with a pen, a book, and a lamp.
  2. One call. cart.clear() removes all three at once.
  3. Empty brackets. The deque now prints as [] with size 0.
  4. 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.

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

How It Works

  1. One deque, created once. buffer is made before the loop.
  2. Round 1. 10 and 11 are added, printed, then cleared.
  3. Round 2. The same deque starts empty again, so it shows only 20 and 21.
  4. 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.

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

How It Works

  1. Two names, one deque. alias = shared copies the reference, not the elements.
  2. Reassigning. alias = new ArrayDeque<>() points alias at a new empty deque; shared still holds [a, b].
  3. Clearing. After pointing alias back at the shared deque, alias.clear() empties the one object both variables use.
  4. Empty behavior. poll() on the cleared deque returns null.

Frequently Asked Questions

It removes all elements, leaving an empty deque.
Yes. It is the same object, now empty, and you can add new elements to it.
clear() empties the existing object, so all references see it empty. Assigning a new deque only changes the one variable.
null, because the deque is empty. pop() and removeFirst() would throw NoSuchElementException.
No. Its return type is void.

Next: clone()

Make a shallow copy of the whole deque.

clone() 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.

11 people found this page helpful