Java · Double-ended queue · container
ArrayDeque<E>
A resizable circular array usable as a queue or a stack: amortized constant time at both ends, no nulls.
import java.util.ArrayDeque; public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable
Java SE 25 (since Java 6)
Operations
| Operation | Time | Worst case | Changes the collection |
|---|---|---|---|
| offer / offerLast / addLast | O(1) | O(n) when growing | Yes |
| poll / pollFirst / removeFirst | O(1) | O(1) | Yes |
| push / pop (stack) | O(1) | O(n) for push when growing | Yes |
| peek / peekFirst / peekLast | O(1) | O(1) | No |
"Time" shows the amortized, expected or average bound when one applies; open an operation for the full profile and its assumptions.
How it works
- Representation
- An Object[] ring buffer with head and tail indices; grows when full (doubling while small, then 1.5×, in OpenJDK).
- Ordering
- Sequence order from head (first) to tail (last).
- Duplicates
- Allowed; null elements are prohibited.
- Mutability
- Mutable; fail-fast iterators. Not thread-safe.
- Memory and ownership
- Capacity ≥ size; never shrinks.
- Equality and comparison
- contains and remove(Object) use equals() (linear).
Pitfalls
- Each operation comes in a throwing form (addFirst, removeFirst, getFirst) and a value-returning form (offerFirst, pollFirst, peekFirst); mixing them up changes empty-deque behaviour.
- null elements throw NullPointerException because null is the "empty" signal of poll and peek.
- There is no index access (get(i)); it is a deque, not a list.
Alternatives
- LinkedList (allows null, worse locality)
- PriorityQueue to serve by priority
- ArrayBlockingQueue / ConcurrentLinkedDeque between threads
- The legacy Stack class is synchronized and discouraged; the Deque documentation recommends ArrayDeque
In other languages
- C#: Queue<T>
- Python: collections.deque
- C++: std::deque<T>