Java ArrayDeque

Beginner
8 min read
Updated: Nov 2026
3 examples
java.util.ArrayDeque

Definition and Usage

In Java, ArrayDeque (in java.util, since Java 1.6) is a double-ended queue: a list of elements that you can add to, remove from, and read at both ends. The front is called the head (or first) and the back the tail (or last). Inside, it keeps the elements in a resizable array that grows by itself, so there is no capacity limit.

It implements Deque, Queue, Collection, and Iterable, and is also Cloneable and Serializable. That makes it a fast general-purpose queue (offer(), poll(), peek()) and a fast stack (push(), pop()). Adding or removing at either end takes constant time on average; searching by value with contains() walks the whole deque. The ArrayDeque Methods index lists every guide.

Two rules to remember.

An ArrayDeque never holds null: adding null throws NullPointerException. And it is not thread-safe: share it between threads only with your own locking. Its iterators are fail-fast: if the deque changes while you loop (other than through the iterator itself), the loop usually stops with ConcurrentModificationException.

Java Syntax

Import the class, then create a deque. Declaring the variable as Deque keeps your code flexible:

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

Deque<String> deque = new ArrayDeque<>();               // empty
Deque<Integer> roomy = new ArrayDeque<>(100);           // starting size hint, not a limit
Deque<String> copy = new ArrayDeque<>(Arrays.asList("a", "b"));  // [a, b]

deque.addFirst("x");                // head
deque.addLast("y");                 // tail
System.out.println(deque);          // [x, y]  (head to tail)

ArrayDeque vs Stack, LinkedList, and ArrayList

All four store elements in order, but they are built for different jobs:

ClassGood forKeep in mind
ArrayDequeQueues and stacks: fast add and remove at both endsNo index access, no null, not thread-safe
StackOld code that already uses itLegacy class built on Vector; every method is synchronized. Prefer ArrayDeque for new code
LinkedListA List and a Deque in one; allows nullOne node object per element, so it usually uses more memory and is slower than ArrayDeque as a queue
ArrayListReading by index with get(i), adding at the endRemoving from the front shifts every other element, so it is a poor queue

Queue or Stack: Which Methods?

The same ArrayDeque can play either role. Pick one set of methods and stick to it:

JobQueue (FIFO)Stack (LIFO)
Addoffer(e) or add(e): at the tailpush(e): at the head
Removepoll() (or remove()): from the headpop(): from the head
Lookpeek() (or element()): the headpeek(): the head

Throw an Exception or Return a Special Value

Each end has two versions of every operation. The left one in each pair throws (NoSuchElementException on an empty deque); the right one returns null instead. For inserts, offerFirst/offerLast return true, because an ArrayDeque never runs out of room.

OperationHead: throwsHead: special valueTail: throwsTail: special value
InsertaddFirst()offerFirst()addLast()offerLast()
RemoveremoveFirst()pollFirst()removeLast()pollLast()
ExaminegetFirst()peekFirst()getLast()peekLast()

Java Examples

Example 1 — Use an ArrayDeque as a Queue (FIFO)

Customers join at the tail with offer() and are served from the head with poll(), so the first to arrive is the first served.

Java
import java.util.ArrayDeque;
import java.util.Queue;

public class DequeAsQueue {
    public static void main(String[] args) {
        Queue<String> counter = new ArrayDeque<>();
        counter.offer("Asha");
        counter.offer("Bilal");
        counter.offer("Chen");

        System.out.println("Waiting: " + counter);
        System.out.println("Next up: " + counter.peek());

        while (!counter.isEmpty()) {
            System.out.println("Serving " + counter.poll());
        }
        System.out.println("poll() on empty: " + counter.poll());
    }
}

How It Works

  1. A queue view of the deque. The variable is typed as Queue<String>, so only the queue methods are visible. The object behind it is still an ArrayDeque.
  2. Join at the tail. Each offer() puts a name at the back, so the printed order [Asha, Bilal, Chen] is the arrival order.
  3. Serve from the head. peek() shows Asha without removing her, and each poll() in the loop removes and returns the current front name.
  4. No exception at the end. When the queue is empty, poll() returns null rather than throwing, which is why the last line prints null.

Example 2 — Use an ArrayDeque as a Stack (LIFO)

Browser-style "back" history: push() puts the newest page on top and pop() goes back to the one before.

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

public class DequeAsStack {
    public static void main(String[] args) {
        Deque<String> back = new ArrayDeque<>();
        back.push("home");
        back.push("products");
        back.push("cart");

        System.out.println("Stack: " + back);
        System.out.println("Top: " + back.peek());

        System.out.println("Back to " + back.pop());
        System.out.println("Back to " + back.pop());
        System.out.println("Left: " + back);
    }
}

How It Works

  1. Push onto the head. push() adds at the front each time, so the newest page, cart, is printed first and the oldest, home, last.
  2. Peek at the top. For a stack, peek() reads the head, which is the most recently pushed element.
  3. Pop in reverse order. Each pop() removes the head, so the pages come back as cart, then products: last in, first out.
  4. A better Stack. This is what the old java.util.Stack class does, but ArrayDeque is not synchronized, so it avoids locking costs in single-threaded code.

Example 3 — Work at Both Ends: Throw vs Return null

Add at both ends, read both ends, then empty the deque and compare pollLast() with removeLast().

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

public class DequeBothEnds {
    public static void main(String[] args) {
        Deque<Integer> nums = new ArrayDeque<>();
        nums.addLast(2);
        nums.addLast(3);
        nums.addFirst(1);

        System.out.println(nums + " first=" + nums.getFirst() + " last=" + nums.getLast());

        nums.clear();
        System.out.println("pollLast: " + nums.pollLast());
        try {
            nums.removeLast();
        } catch (NoSuchElementException e) {
            System.out.println("removeLast: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Fill from both sides. Two addLast calls give [2, 3], and addFirst(1) puts 1 in front of them.
  2. Read both ends. getFirst() and getLast() return 1 and 3 and leave all three numbers in place.
  3. Special value. After clear(), pollLast() has nothing to return, so it gives null.
  4. Exception. removeLast() is the throwing twin of pollLast(), so on the empty deque it throws NoSuchElementException, which the catch block reports by class name.

Next: Java ArrayDeque Methods by Group

Full list with short descriptions: Java ArrayDeque Methods index (/java/array-deque/method).

Frequently Asked Questions

ArrayDeque is a class in java.util that stores elements in a resizable array and lets you add, remove, and read them at both ends. It implements the Deque and Queue interfaces, so it works as a queue or as a stack.
No. Every method that adds an element throws NullPointerException for null. That is why poll() and peek() can safely use null to mean the deque is empty.
No. Use it from one thread, or protect it yourself. For a queue shared between threads, look at the concurrent classes in java.util.concurrent, such as ConcurrentLinkedDeque or LinkedBlockingDeque.
The Java documentation recommends Deque implementations such as ArrayDeque over the legacy Stack class. ArrayDeque is not synchronized, so it is usually faster, and push, pop, and peek work the same way.
No. It has no capacity limit and grows as needed. new ArrayDeque<>(100) only gives a starting size hint; the deque can still grow beyond it.

Browse every ArrayDeque method

Open the methods index for categorized guides with examples and FAQs.

ArrayDeque Methods Index →

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.

9 people found this page helpful