java.util · java.base · since Java 1.5

Queue

declaration
public interface Queue<E> extends Collection<E>

A holding collection with head-first removal. Two method families: throwing (add/remove/element) and value-returning (offer/poll/peek) — pick per how exceptional emptiness is.

  • Two parallel method families cover the same three operations: offer/poll/peek return a special value (false/null) on failure; add/remove/element throw instead — pick the family that matches how exceptional emptiness or fullness is for the call site.

Key methods

boolean offer(E)Insert — for capacity-restricted queues, returns false on failure instead of throwing.
E poll() / E peek()Remove-and-return / inspect the head — return null if the queue is empty.
boolean add(E)Insert — throws IllegalStateException if the queue is capacity-restricted and full.
E remove() / E element()Remove-and-return / inspect the head — throw NoSuchElementException if the queue is empty.