ArrayDeque<E> · Java
poll / pollFirst / removeFirst
Removes from the head: the dequeue operation.
public E pollFirst(); public E removeFirst(); E poll() // == pollFirst
Complexity
| Best case | O(1) |
|---|---|
| Worst case | O(1) |
| Extra space | O(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
}
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.