Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Stack vs ArrayDeque: Choosing the Right Stack Implementation

Compare Java's legacy Stack class with ArrayDeque for LIFO stack operations: synchronization, null handling, performance, and when to choose each.

~3 min readJava
#Java#Stack#ArrayDeque#Collections
Diagram comparing Java Stack and ArrayDeque implementations for stack operations.

Choosing between java.util.Stack and java.util.ArrayDeque is a common question when you need LIFO behavior in Java. Both classes support the core push/pop operations, but they differ in synchronization, null handling, and intended use. This article compares the two and gives practical guidance for choosing one.

The Legacy Stack Class and Its Limitations

Java's java.util.Stack class has existed since JDK 1.0. It extends Vector and provides push, pop, peek, empty, and search methods for LIFO access. Because Stack inherits from Vector, its methods are synchronized: each call acquires a lock on the stack. That was reasonable in early Java, but in modern single-threaded code it adds overhead that is rarely needed. The class is not formally deprecated, but it is widely considered legacy in favor of the Java Collections Framework's Deque implementations.

ArrayDeque: The Modern Replacement

java.util.ArrayDeque is a resizable-array implementation of the Deque interface. You can use it as a stack with push and pop, which are equivalent to addFirst and removeFirst. It is not synchronized, so it avoids the locking overhead of Stack. ArrayDeque also does not allow null elements, a deliberate design decision that prevents ambiguity when collection methods use null as a special return value.

Key Differences Between Stack and ArrayDeque

FeatureStackArrayDeque
Thread safetySynchronizedNot synchronized
Null elementsAllowedNot allowed
Synchronization overheadPresentNone
InheritanceExtends VectorImplements Deque
Iterator orderBottom to topTop to bottom (head to tail)

The difference that matters most in practice is thread safety. Stack is synchronized, so every method call acquires a lock. In a single-threaded application this adds unnecessary overhead. ArrayDeque has no synchronization, making it the usual choice in single-threaded code.

Performance Considerations

Because Stack inherits from Vector, its operations are synchronized. Each push or pop acquires a monitor lock, which can become a bottleneck in high-throughput scenarios. ArrayDeque avoids this synchronization overhead. It also uses a circular array that grows dynamically, while Stack relies on Vector's capacity management. In single-threaded code, ArrayDeque avoids synchronization cost and is generally the better-performing option.

When to Use Stack vs ArrayDeque

Use ArrayDeque for new code that needs a stack and runs in a single-threaded environment. It is the recommended implementation from the Java Collections Framework. Use Stack only when maintaining legacy code that already depends on it. Its per-method synchronization does not make compound push/pop sequences atomic, so it is not a complete thread-safety solution on its own.

For concurrent access, neither class is ideal. If you need a thread-safe deque, consider ConcurrentLinkedDeque for lock-free, non-blocking operations or LinkedBlockingDeque for blocking behavior. You can also wrap an ArrayDeque with Collections.synchronizedDeque, but the wrapper still requires care for compound operations.

Implementing a Stack with Both Classes

Here is a simple example that demonstrates the same LIFO behavior:

java
import java.util.ArrayDeque; import java.util.Stack; public class StackComparison { public static void main(String[] args) { // Using Stack Stack<String> stack = new Stack<>(); stack.push("first"); stack.push("second"); System.out.println(stack.pop()); // second // Using ArrayDeque as a stack ArrayDeque<String> deque = new ArrayDeque<>(); deque.push("first"); deque.push("second"); System.out.println(deque.pop()); // second } }

Both produce identical results for basic operations. The key difference is that ArrayDeque does not allow null elements. If you try to push(null), it throws NullPointerException. This can be a benefit because it prevents accidental null values in your stack.

Handling Null Elements and Edge Cases

If your stack must allow null, Stack is the only option among these two. However, allowing null values often leads to bugs. In the collections framework, null is sometimes used as a special return value; for example, Deque.poll() returns null when the deque is empty. ArrayDeque avoids ambiguity by rejecting nulls. If you need a deque that allows nulls, LinkedList implements Deque and permits null, but it has higher per-element memory overhead than ArrayDeque.

Final Recommendation

In modern Java code, ArrayDeque should be your default choice for a stack. It avoids synchronization overhead, supports the full Deque API, and is part of the standard collections framework. Stack remains for legacy compatibility, but its inheritance from Vector and its synchronized methods make it a poor choice for new development. Consider your concurrency and null-handling requirements before selecting an implementation.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.