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 ArrayDequenever 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.
Syntax
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)
Reference
ArrayDeque vs Stack, LinkedList, and ArrayList
All four store elements in order, but they are built for different jobs:
Class
Good for
Keep in mind
ArrayDeque
Queues and stacks: fast add and remove at both ends
No index access, no null, not thread-safe
Stack
Old code that already uses it
Legacy class built on Vector; every method is synchronized. Prefer ArrayDeque for new code
LinkedList
A List and a Deque in one; allows null
One node object per element, so it usually uses more memory and is slower than ArrayDeque as a queue
ArrayList
Reading by index with get(i), adding at the end
Removing from the front shifts every other element, so it is a poor queue
Reference
Queue or Stack: Which Methods?
The same ArrayDeque can play either role. Pick one set of methods and stick to it:
Each end has two versions of every operation. The left one in each pair throws (NoSuchElementException on an empty deque); the right one returnsnull instead. For inserts, offerFirst/offerLast return true, because an ArrayDeque never runs out of room.
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());
}
}
Output
Waiting: [Asha, Bilal, Chen]
Next up: Asha
Serving Asha
Serving Bilal
Serving Chen
poll() on empty: null
How It Works
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.
Join at the tail. Each offer() puts a name at the back, so the printed order [Asha, Bilal, Chen] is the arrival order.
Serve from the head.peek() shows Asha without removing her, and each poll() in the loop removes and returns the current front name.
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);
}
}
Output
Stack: [cart, products, home]
Top: cart
Back to cart
Back to products
Left: [home]
How It Works
Push onto the head.push() adds at the front each time, so the newest page, cart, is printed first and the oldest, home, last.
Peek at the top. For a stack, peek() reads the head, which is the most recently pushed element.
Pop in reverse order. Each pop() removes the head, so the pages come back as cart, then products: last in, first out.
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().
Fill from both sides. Two addLast calls give [2, 3], and addFirst(1) puts 1 in front of them.
Read both ends.getFirst() and getLast() return 1 and 3 and leave all three numbers in place.
Special value. After clear(), pollLast() has nothing to return, so it gives null.
Exception.removeLast() is the throwing twin of pollLast(), so on the empty deque it throws NoSuchElementException, which the catch block reports by class name.
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.