ArrayDeque is a resizable-array deque for inserting and removing elements at either end.
Java ArrayDeque for queues and stacks
FIFO work order
import java.util.ArrayDeque;
import java.util.Deque;
public class TaskDequeDemo {
public static void main(String[] args) {
Deque<String> pendingTasks = new ArrayDeque<>();
pendingTasks.addLast("Validate invoice");
pendingTasks.addLast("Send receipt");
System.out.println(pendingTasks.pollFirst());
System.out.println(pendingTasks.pollFirst());
System.out.println(pendingTasks.pollFirst());
}
}The output is Validate invoice, Send receipt, and null. A stack instead pairs push with pop at the same end. Do not mix that convention with addLast and assume the result is FIFO.
Most endpoint operations are amortized O(1). The deque has no List-style get(index) operation. It rejects null elements, which leaves null available as an empty result for poll and peek.
Endpoint absence and memory retention
ArrayDeque rejects null elements, so poll returning null can clearly mean no element was available. It is not a blocking queue and does not wait for a producer to add another element.
A deque can grow to absorb a burst and may retain backing-array capacity after removals. Empty size is not proof that all storage returned to the runtime. Bound queued work according to the application’s memory budget.
Connected lessons
Continue with Java LinkedList: operations, internals and failure cases, Java LinkedList quiz.
Common Mistakes
ArrayDeque is not thread-safe and has no fixed capacity limit. Use a bounded or concurrent queue when producers, backpressure, or multiple consumers require those properties.
