In Java, a queue is a first-in, first-out (FIFO) collection and a stack is a last-in, first-out (LIFO) collection. For both, use ArrayDeque, not the old Stack class. Stack extends Vector, is synchronized on every call and is documented as legacy, while the Deque interface gives you proper stack and queue methods. This guide explains queue and stack java usage and why ArrayDeque wins.
Both structures are about the order in which elements leave. A queue serves the oldest element first, like a line at a shop. A stack serves the newest element first, like a pile of plates.
Queue<E> adds methods for inserting at the tail and removing from the head. Deque<E> ("double-ended queue", pronounced "deck") extends Queue and allows insertion and removal at both ends, so it can act as a queue or a stack.
| Operation | Throws on failure | Returns special value |
|---|---|---|
| Insert | add(e) | offer(e) |
| Remove head | remove() | poll() |
| Examine head | element() | peek() |
The offer, poll and peek forms return false or null instead of throwing, which is usually what you want in application code. The common implementations are ArrayDeque, LinkedList, and PriorityQueue (which orders by priority, not by arrival).
For a FIFO queue, add at the tail with offer and take from the head with poll.
ArrayDeque is backed by a resizable circular array, so it is fast and compact. It does not accept null elements, because null is the "empty" return value of poll and peek.
For a LIFO stack, use the Deque methods push, pop and peek. They work on the head of the deque, so the last pushed element comes out first.
Note the printed order: a Deque used as a stack prints from the top, so the newest element is first. pop() and remove() throw NoSuchElementException on an empty deque; use pollFirst() or peek() to get null instead.
A classic use is checking balanced brackets:
java.util.Stack has existed since Java 1.0. It extends Vector, which causes real problems:
Vector, it exposes get(i), add(i, e) and remove(i), so callers can break the LIFO discipline.ArrayDeque.Deque states that this interface should be used in preference to the legacy Stack class.Stack is not removed and still works, but there is no reason to use it in new code.
| Type | Use it when |
|---|---|
ArrayDeque | You need a plain queue, stack or double-ended queue (the default choice) |
LinkedList | You need null elements or list methods together with queue methods |
PriorityQueue | You need the smallest or largest element first, not insertion order |
ConcurrentLinkedQueue, LinkedBlockingQueue | Several threads share the queue |
ArrayDeque is not thread-safe. For producer-consumer code across threads, use a BlockingQueue implementation from java.util.concurrent. PriorityQueue iteration order is not sorted; only poll returns elements in priority order.
A queue removes the oldest element first (FIFO). A stack removes the newest element first (LIFO).
Use Deque, normally ArrayDeque. The Stack class is legacy, synchronized and exposes list operations that do not belong on a stack.
Methods such as poll and peek return null to mean the deque is empty. Allowing null elements would make that signal ambiguous.
It works, because it implements Deque, but each element needs a separate node object, so ArrayDeque is usually faster and uses less memory.
Use ArrayDeque for both queues and stacks in modern Java: offer, poll and peek for FIFO, push, pop and peek for LIFO. Prefer the Deque interface type, avoid java.util.Stack, choose PriorityQueue for priority order, and pick a concurrent queue when threads share data.