ArrayDeque<E> · Java

poll / pollFirst / removeFirst

Removes from the head: the dequeue operation.

public E pollFirst(); public E removeFirst(); E poll() // == pollFirst

Complexity

Best caseO(1)
Worst caseO(1)
Extra spaceO(1)

Read at the head index, null it, advance the head.

Tested example

Queue<Integer> queue = new ArrayDeque<>(List.of(1, 2));
assert queue.poll() == 1;   // first in, first out
assert queue.poll() == 2;
assert queue.poll() == null; // empty: poll returns null...
try {
    queue.remove();          // ...remove throws
    throw new AssertionError("expected NoSuchElementException");
} catch (NoSuchElementException expected) {
    // empty queue
}
Runs in the test suite on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/test/java/dev/bekadan/dsa/collections/ArrayDequeExamples.java

Behaviour

Returns
The head element; pollFirst returns null when empty.
Errors and preconditions
removeFirst / remove: NoSuchElementException when empty.
Changes
size decreases by 1; the slot is nulled.
Memory and invalidation
No allocation.