Quick revision
The short answer
ArrayDeque is a resizable circular array with amortized O(1) insertion and removal at both ends, so it works as both a stack and a queue. Stack extends Vector, which makes every call synchronized and exposes index-based methods that break stack discipline, and LinkedList allocates a node object per element, which costs memory and cache locality. ArrayDeque rejects nulls and is not thread-safe; we use push, pop and peek for a stack and offer, poll and peek for a queue.
What to remember
In this answer, we saw that ArrayDeque stores elements in a circular array with head and tail indexes, giving amortized O(1) adds and removes at both ends with no shifting and little memory. Stack extends Vector, so every call is synchronized and its list methods let callers break stack discipline, and LinkedList allocates a node per element, costing memory, garbage and cache locality. ArrayDeque rejects nulls and is not thread-safe, and we use push, pop and peek for stacks and offer, poll and peek for queues.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ArrayDeque is a resizable circular array with amortized O(1) insertion and removal at both ends, so it works as both a stack and a queue. Stack extends Vector, which makes every call synchronized and exposes index-based methods that break stack discipline, and LinkedList allocates a node object per element, which costs memory and cache locality. ArrayDeque rejects nulls and is not thread-safe; we use push, pop and peek for a stack and offer, poll and peek for a queue.
Why this question is asked
Stacks and queues are everywhere: undo histories, expression parsers, depth-first and breadth-first searches, work lists in schedulers. Java has three classes people reach for, and the Javadoc of Stack itself points to Deque, with ArrayDeque as the usual implementation. Interviewers ask why, because the answer covers memory layout, synchronization cost and API design in one question. In this answer, we will recap what a deque is, see how ArrayDeque's circular array works, look at what is wrong with Stack and with LinkedList for this job, and finish with nulls, thread safety and the right methods to call.
1A Quick Recap of Stacks, Queues and Deques
A stack is last in, first out: we push onto the top and pop from the top, like a pile of plates. A queue is first in, first out: we add at the tail and remove from the head, like a line at a counter. A deque, short for double-ended queue, allows adding and removing at both ends, so it can play either role.
Java models this with the Deque interface, added in Java 6. It extends Queue and adds methods for both ends, including the stack methods push, pop and peek, which work on the head. Because a Deque can be used as a stack, the Stack class is no longer needed, and its own Javadoc recommends a Deque instead.
2How ArrayDeque's Circular Array Works
ArrayDeque keeps elements in an Object array with two indexes: head, the position of the first element, and tail, the position just after the last one. Adding at the tail writes to tail and moves it forward; adding at the head moves head backward and writes there. Removing does the reverse. When an index runs off one end of the array, it wraps around to the other end, which is why the array is called circular. Nothing is ever shifted, so each operation touches a single slot.
| Slot | Content | Note |
|---|---|---|
| 0 | 1 | first element added with offerLast |
| 1 | 2 | second element |
| 2 | (empty) | tail points here: next offerLast writes here |
| 3 to 6 | (empty) | free space between tail and head |
| 7 | 0 | head: offerFirst wrapped around to the last slot |
When the array is full, ArrayDeque allocates a larger one, roughly doubling while it is small and growing by half once it is large, and copies the elements across in order. A single growth copies everything, but it happens rarely, and spread over all insertions the cost per element is constant. That is what amortized O(1) means here.
3What Is Wrong With Stack
Stack dates from Java 1.0 and extends Vector, a resizable array whose methods are all synchronized. Every push, pop and peek acquires a monitor lock. Without contention, a modern JVM makes this fairly cheap, but it is still work and it prevents some optimizations, and it buys nothing, because a stack used by one thread does not need a lock, while a stack shared by several threads usually needs compound operations, like check-then-pop, that per-method locking does not protect anyway.
The bigger design problem is inheritance. Because Stack is a Vector, it exposes every list method: get(int), add(int, E), remove(int), set and insertElementAt. Any caller can read or modify the middle of the stack, which breaks the stack discipline that the type promises. Even iteration surprises people.
Notice that the two print in opposite orders. A Deque used as a stack iterates from the top, which matches how we think about a stack, while Stack iterates from the bottom because it is really a list.
4What Is Wrong With LinkedList
LinkedList also implements Deque, so it works as a stack or queue with the same methods. The problem is its memory layout. Every element needs a separate node object holding the element reference and two links, previous and next. On a 64-bit JVM with compressed references, a node takes about 24 bytes, on top of the element itself, while ArrayDeque spends about 4 bytes per element for a slot in its array.
Allocation is the other cost. Each add creates a node and each remove turns one into garbage, so a busy queue produces a steady stream of short-lived objects for the garbage collector. And because nodes are scattered in the heap, walking or polling them causes cache misses, whereas ArrayDeque's elements sit next to each other in one array. In benchmarks of plain stack and queue use, ArrayDeque is usually clearly faster than LinkedList. LinkedList also accepts null elements, which makes poll returning null ambiguous.
5Nulls, Thread Safety and the Right Methods
ArrayDeque does not allow null elements; adding one throws NullPointerException. The reason is practical: an empty slot in its array is null, and methods like poll and peek return null to mean empty, so a stored null would be indistinguishable from no element. ArrayDeque is also not thread-safe. For concurrent use we pick ConcurrentLinkedDeque for a non-blocking deque or LinkedBlockingDeque when consumers should wait for elements.
Finally, we should use the method names that say what we mean. For a stack, push, pop and peek all work on the head. For a queue, offer adds at the tail, poll removes from the head and peek reads the head. Mixing them, such as push with poll, gives a stack, which may not be what the reader expects. pop and remove throw NoSuchElementException on an empty deque, while poll and peek return null.
6Follow-up questions
Is ArrayDeque always better than LinkedList?
For stack and queue use, almost always. LinkedList can still make sense when we need to remove elements from the middle while iterating with a ListIterator, or when we need a List and a Deque in one object. For pure end operations, ArrayDeque is smaller and faster.
Why does the stack view iterate top first?
Because push adds at the head of the deque, and iteration always goes from head to tail. The most recently pushed element is at the head, so it comes first, just as pop would return it.
Should we pre-size an ArrayDeque?
When we know roughly how many elements it will hold at once, yes: new ArrayDeque<>(expected) avoids several rounds of growth and copying. For small or unknown sizes, the default is fine because growth is amortized.
7Conclusion
In this answer, we saw that ArrayDeque stores elements in a circular array with head and tail indexes, giving amortized O(1) adds and removes at both ends with no shifting and little memory. Stack extends Vector, so every call is synchronized and its list methods let callers break stack discipline, and LinkedList allocates a node per element, costing memory, garbage and cache locality. ArrayDeque rejects nulls and is not thread-safe, and we use push, pop and peek for stacks and offer, poll and peek for queues.
Open with
ArrayDeque is a resizable circular array: it keeps a head and a tail index into an Object array, and adding or removing at either end just writes or clears one slot and moves an index, wrapping around at the end of the array.
Then, if they want more
When it is full it grows and copies, but that is rare, so adds and removes at both ends are amortized O(1). That makes it a good stack and a good queue at the same time.
Stack is a legacy class that extends Vector, so every method is synchronized, which is overhead for single-threaded use and still does not make compound operations safe. Worse, because it is a Vector, it exposes index-based methods like get, add at an index and remove at an index, so callers can break stack discipline, and it even iterates bottom first.
LinkedList implements Deque too, but every element needs its own node object of about 24 bytes with two links, so it uses more memory, creates garbage on every add and remove, and has poor cache locality compared with ArrayDeque's contiguous array.
Two caveats: ArrayDeque does not allow null elements, because null marks an empty slot and poll returns null for empty, and it is not thread-safe, so for concurrent use I would pick ConcurrentLinkedDeque or LinkedBlockingDeque. And I use push, pop and peek for stack behaviour and offer, poll and peek for queue behaviour, so the intent is clear.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why does the Javadoc recommend ArrayDeque over Stack and LinkedList for stack and queue...": https://scrivio-interview-library.netlify.app/java/queues-and-deques/314573d7ce/