java.util · java.base · since Java 1.6
ArrayDeque
public class ArrayDeque<E> extends AbstractCollection<E>
implements Deque<E>, Cloneable, SerializableCircular-array Deque — the right default for both stacks and queues: faster than java.util.Stack (unsynchronized) and than LinkedList (no nodes).
- Null elements are forbidden (null is the "empty" sentinel for poll)
- Not thread-safe — synchronize externally or use a concurrent Deque implementation across threads.
Key methods
ArrayDeque() | Default capacity, backed by a resizable circular array. |
ArrayDeque(int numElements) | Presize to skip regrowth copies. |
Example
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop(); // 3 — LIFO
Deque<Integer> queue = new ArrayDeque<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.poll(); // 1 — FIFO