ArrayDeque vs.
LinkedList
Choosing the right Deque implementation in Java: when a resizable array beats a linked chain and vice versa.
What Is a Deque?
A Deque (double-ended queue) is a linear collection that supports insertion and removal at both ends. In Java, the Deque<E> interface lives in java.util and extends Queue<E>.
Inside ArrayDeque
ArrayDeque stores elements in a resizable circular array. Two integer indices, head and tail, track the front and back, wrapping around when they reach the end of the array.
Inside LinkedList
LinkedList is a doubly-linked list. Each element lives in its own Node object that holds pointers to the previous and next nodes.
Why the Choice Matters
Both classes implement the same interface, so they are interchangeable in code. But internally they differ in memory layout, constant factors, and special capabilities. Picking the wrong one can cost you 2-4x performance in tight loops.
Time Complexity: Head-to-Head
| Operation | ArrayDeque | LinkedList |
|---|---|---|
| addFirst / addLast | Amortized \( O(1) \) | \( O(1) \) |
| removeFirst / removeLast | \( O(1) \) | \( O(1) \) |
| peekFirst / peekLast | \( O(1) \) | \( O(1) \) |
| get(index) | Not supported | \( O(n) \) |
| Iteration (full scan) | \( O(n) \) cache-friendly | \( O(n) \) pointer-chasing |
| Memory per element | ~1 reference | ~3 references |
The Key Distinction
null Elements
ArrayDeque uses null internally as a sentinel to detect empty slots. If you try to add null, it throws a NullPointerException. LinkedList has no such restriction.
How to Choose
Example 1: Undo Stack
You are building a text editor with an undo feature. Every action is pushed onto a stack, and undo pops the most recent one. No nulls, no indexed access needed.
Example 2: BFS Queue
Breadth-first search processes nodes in FIFO order. You enqueue at the back, dequeue from the front. Nodes are never null.
Example 3: When LinkedList Wins
A music player maintains a playlist that users can navigate forward/backward (deque) and also jump to by index (list). Null entries represent "ad breaks."
Cache Locality Explained
ArrayDeque Resizing
When the internal array is full, ArrayDeque doubles its capacity and copies all elements to a new array. This single expensive \( O(n) \) copy is spread across all the previous insertions, yielding amortized \( O(1) \).
Common Mistakes
Summary Comparison
| Feature | ArrayDeque | LinkedList |
|---|---|---|
| Backing structure | Resizable circular array | Doubly-linked nodes |
| Implements List? | No | Yes |
| Allows null? | No | Yes |
| Memory efficiency | Better | Worse (node overhead) |
| Cache performance | Excellent | Poor |
| Best for stacks/queues | ✓ Recommended | Acceptable |
| Best for List + Deque | Cannot | ✓ Only option |
Question 1 of 5
You need a LIFO stack for parsing matched parentheses. No nulls are stored. Which implementation should you choose?
Question 2 of 5
Your collection must support both indexed access (get by position) and deque operations (addFirst, removeLast). Which is the better choice?
Question 3 of 5
Why does ArrayDeque generally outperform LinkedList for queue operations, even though both have \( O(1) \) add/remove?
Question 4 of 5
What happens when you call arrayDeque.add(null)?
Question 5 of 5
You are implementing a BFS traversal of a graph with 100,000 nodes. No null values are needed. Which implementation minimizes memory usage?