What is ArrayDeque
?
ArrayDeque
(Array Double Ended Queue) is a resizable-array implementation of the Deque
interface in Java.
Key Features:
- Allows insertion and deletion at both ends (head and tail)
- No capacity restrictions (grows dynamically as needed)
- Faster than
Stack
andLinkedList
for stack/queue operations - Not thread-safe (like most collection classes in Java unless explicitly synchronized)
Where is it defined?
It is part of the Java Collections Framework, available since Java 6.
import java.util.ArrayDeque;
Class Declaration
public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable
E
is the type of element stored- Implements the
Deque
interface, so it supports both queue and stack operations
Constructors
Constructor | Description |
---|---|
ArrayDeque() | Creates an empty deque with initial capacity 16 |
ArrayDeque(int numElements) | Creates a deque with specified initial capacity |
ArrayDeque(Collection<? extends E> c) | Creates a deque initialized with elements from the given collection |
Common Operations
Method | Description |
---|---|
addFirst(E e) | Inserts at the front |
addLast(E e) | Inserts at the end |
offerFirst(E e) | Same as addFirst() but returns false if it fails instead of throwing exception |
offerLast(E e) | Same as addLast() |
removeFirst() | Removes and returns the first element |
removeLast() | Removes and returns the last element |
pollFirst() | Like removeFirst() , but returns null if deque is empty |
pollLast() | Like removeLast() |
peekFirst() | Returns front element without removing |
peekLast() | Returns rear element without removing |
push(E e) | Same as addFirst() (used for stack) |
pop() | Same as removeFirst() (used for stack) |
Queue Example (FIFO)
import java.util.ArrayDeque; public class QueueExample { public static void main(String[] args) { ArrayDeque<String> queue = new ArrayDeque<>(); queue.addLast("Alice"); queue.addLast("Bob"); queue.addLast("Charlie"); System.out.println("Front: " + queue.peekFirst()); while (!queue.isEmpty()) { System.out.println("Dequeued: " + queue.removeFirst()); } } }
Output:
Front: Alice Dequeued: Alice Dequeued: Bob Dequeued: Charlie
Stack Example (LIFO)
import java.util.ArrayDeque; public class StackExample { public static void main(String[] args) { ArrayDeque<Integer> stack = new ArrayDeque<>(); stack.push(10); stack.push(20); stack.push(30); System.out.println("Top: " + stack.peek()); while (!stack.isEmpty()) { System.out.println("Popped: " + stack.pop()); } } }
Output:
Top: 30 Popped: 30 Popped: 20 Popped: 10
When to Use ArrayDeque
Use ArrayDeque
when you need:
- A stack (LIFO) with better performance than the
Stack
class - A queue (FIFO) with better performance than
LinkedList
- In Breadth-First Search (BFS) or similar algorithms where you frequently add/remove from both ends
What ArrayDeque
Doesn’t Support
- Null elements: You cannot add
null
values. It throwsNullPointerException
. - Thread safety: Not synchronized. Use
Collections.synchronizedDeque()
or another thread-safe implementation if needed. - Random access: Unlike
ArrayList
, you cannot access elements by index.
ArrayDeque performance comparison with Stack
Because Stack
is synchronized (because it extends the Vector
class) this is why it is going to be slower than the ArrayDeque
solution. So it is advisable to use ArrayDeque if we are after a LIFO structure.