java.util · java.base · since Java 1.6

Deque

declaration
public interface Deque<E> extends Queue<E>, SequencedCollection<E>

Double-ended queue: efficient add/remove at both ends. Serves as both the modern Stack (push/pop) and a plain Queue.

  • Deque is the modern replacement for the legacy Stack class — push/pop/peek give the same LIFO discipline without Stack's synchronization overhead or its ill-advised extension of Vector.

Key methods

void addFirst(E) / addLast(E)Throwing insert at either end — IllegalStateException if capacity-restricted and full.
boolean offerFirst(E) / offerLast(E)Capacity-safe insert at either end — returns false instead of throwing.
E removeFirst() / removeLast()Throwing removal from either end — NoSuchElementException if empty.
E pollFirst() / pollLast()Null-returning removal from either end.
E getFirst() / getLast()Throwing inspection of either end — NoSuchElementException if empty.
E peekFirst() / peekLast()Null-returning inspection of either end.
void push(E) / E pop()Stack aliases: push = addFirst, pop = removeFirst (throws if empty).
Iterator<E> descendingIterator()Iterate tail-to-head.