Why is ArrayDeque recommended over the legacy java.util.Stack class for stack behavior?
answer
- Stack extends Vector -> synchronized + index/middle operations leak in
- Stack iteration is bottom-to-top (surprising); abstraction is broken
- ArrayDeque = circular array, unsynchronized, O(1) both ends
- Javadoc: likely faster than Stack and LinkedList
- Concurrent need -> ConcurrentLinkedDeque/BlockingDeque, NOT Stack
basics
~10 sStack is old and extends Vector, so every stack operation takes a lock and it exposes index-based methods that break stack semantics. ArrayDeque is likely faster, unsynchronized, and gives a clean push/pop/peek stack API.
solid answer
~50 sjava.util.Stack is a legacy class from Java 1.0 that extends Vector. That inheritance is its core flaw: every stack call takes a lock, adding overhead even in single-threaded code, and because it IS-A Vector it inherits index-based and middle-of-list operations (get(i), add(i, e), insertElementAt) that violate the LIFO abstraction a stack is supposed to enforce. Its iteration order is also bottom-to-top, the opposite of what most people expect when 'iterating a stack'. ArrayDeque, introduced in Java 6, implements Deque on a resizable circular array. Used as a stack via push/pop/peek it takes no lock per call, and its Javadoc calls it likely to be faster than Stack; it has no index-based methods, and Stack's own Javadoc says to prefer a Deque such as ArrayDeque. The main caveat: ArrayDeque is not thread-safe, so for genuine concurrent use you'd reach for a concurrent structure (e.g. ConcurrentLinkedDeque or a BlockingDeque), not Stack either.
code
java · 11 lines// Legacy: synchronized, leaks Vector operations
Stack<Integer> legacy = new Stack<>();
legacy.push(1);
legacy.add(0, 99); // index op from Vector -> breaks LIFO abstraction!
// Preferred: clean, fast, unsynchronized stack
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1);
stack.push(2);
int top = stack.pop(); // 2, LIFO
// stack.get(0); // does not compile - no index accessgo deeper
Knows ArrayDeque is the modern choice, likely faster, and Stack is old; can use push/pop/peek.
Explains that Stack extends Vector (synchronized, leaks index operations, odd iteration order) and that ArrayDeque is an unsynchronized circular array.
Discusses performance (amortized O(1) at both ends, lock overhead), the JDK recommendation, the null difference, and that Stack's synchronization is insufficient anyway.
Frames it as an abstraction/inheritance design lesson (Stack IS-A Vector is bad LSP/composition), and prescribes the right concurrent alternatives (ConcurrentLinkedDeque/BlockingDeque) for real concurrency needs.
## The two classes **`java.util.Stack<E>`** is a class from Java 1.0 that models a LIFO stack with `push`, `pop`, `peek`, `empty`, and `search`. **`java.util.ArrayDeque<E>`** is a Java 6 class implementing the `Deque` interface; used as a stack it offers `push`, `pop`, and `peek`. Modern Java guidance and the JDK Javadoc itself recommend `ArrayDeque` over `Stack`. `Deque`'s Javadoc calls `Stack` legacy, yet neither `Stack` nor `Vector` is `@Deprecated` at JDK 27 or 25. ## Why Stack is considered legacy ### 1. It extends Vector `Stack extends Vector`. `Vector` is a Java 1.0 resizable array whose methods mostly lock the object's monitor, and **every `Stack` operation takes that lock**: `pop`, `peek` and `search` are `synchronized`, and `push` and `empty` go through the `synchronized` `addElement` and `size`. Consequences: - **Unnecessary locking overhead.** Even in single-threaded code you pay for synchronization on every `push`/`pop`. Biased locking once reduced that uncontended cost; JDK 15 turned it off by default, naming `Vector` among the APIs affected, and the flag is gone at JDK 25 and 27. - **Broken abstraction.** Because `Stack` IS-A `Vector`, it inherits operations that have nothing to do with a stack: `get(int)`, `add(int, E)`, `insertElementAt`, `remove(int)`, `set(int, E)`. A caller can reach into the middle of the 'stack' and mutate it, defeating the LIFO contract. A clean stack should expose only push/pop/peek. - **Surprising iteration order.** Iterating a `Stack` (it IS-A List) goes **bottom-to-top** (insertion order), which is the opposite of popping order. People expect to iterate from the top. ### 2. Synchronization is the wrong default Single-thread synchronization is pure cost with no benefit, and the per-method locking of `Vector`/`Stack` is *not* even sufficient for compound operations (check-then-act still needs external locking), so it gives a false sense of safety while charging for it. ## Why ArrayDeque is better `ArrayDeque` is backed by a **resizable circular array** (a single array with head/tail indices that wrap around). Benefits: - **No synchronization** — no per-operation lock overhead in the common single-threaded case. - **Amortized O(1) at both ends** — `push` and `pop` write or clear one cell and move an index, with an occasional copy to grow. Locality is not the edge here, because `Vector` is an array too. - **Cleaner API** — `Deque` has no indexed access, so positional reads and writes are gone. It is not sealed: `remove(Object)`, `removeFirstOccurrence` and `removeIf` can still take an element from the middle. - **Versatile** — the same class is an efficient FIFO queue *and* a LIFO stack. - **Recommended by the JDK** — `ArrayDeque`'s Javadoc says it is likely to be faster than `Stack` when used as a stack (and faster than `LinkedList` when used as a queue), and the `Deque` and `Stack` Javadoc both say to use `Deque` in preference to `Stack`. ## The important caveat: thread safety `ArrayDeque` is **not** synchronized. If multiple threads mutate it without external synchronization the behavior is undefined. But the answer to 'I need a concurrent stack' is **not** to fall back to `Stack` — it is to use a purpose-built concurrent structure such as `java.util.concurrent.ConcurrentLinkedDeque` (lock-free) or a `BlockingDeque` implementation. `Stack`'s per-call locking does not make compound operations atomic. ## nulls `ArrayDeque` **forbids null** elements (they would collide with the null-means-empty signal of `peek`/`poll`), throwing `NullPointerException`. Not every deque does: `LinkedList` permits them. `Stack`/`Vector` *do* allow null, another subtle behavioral difference. ## One-line summary Prefer `ArrayDeque` (clean, unsynchronized, likely faster) for single-threaded stacks and queues; use a `java.util.concurrent` deque when you genuinely need concurrency. Treat `Stack` (and raw `Vector`) as legacy.
- If you genuinely need a thread-safe stack/deque, what should you use instead of Stack?A concurrent structure such as ConcurrentLinkedDeque (lock-free, non-blocking) or a BlockingDeque (e.g. LinkedBlockingDeque) when you need blocking/capacity semantics. Stack's per-call synchronization is still insufficient for compound check-then-act operations.
- What is the data structure behind ArrayDeque and why does it help performance?A resizable circular array with head/tail indices that wrap around. The ends move by index, so insertion and removal at both ends run in amortized O(1) with no node allocated per element. The array holds references, and against Stack locality is no edge, because Vector is an array too.
- Recommending Stack because it is 'thread-safe' (its per-call locking does not cover compound ops)
- Saying ArrayDeque is thread-safe (it is not)
- Claiming ArrayDeque allows null elements (it forbids them)
- Not knowing Stack extends Vector, which is the root of the criticism
- java.util.ArrayDeque
- Java 1.6
- java.util.Stack
- Java 1.0
Assumes JDK 27. Stack.java, Vector.java and ArrayDeque.java are identical at JDK 25. Biased locking, which once reduced uncontended locking cost, has been off by default since JDK 15, and its flag is gone at both.
context
- ArrayDeque (Java SE 27 & JDK 27)docs.oracle.com
- Stack (Java SE 27 & JDK 27)docs.oracle.com
- Deque (Java SE 27 & JDK 27)docs.oracle.com