ArrayDeque<E> · Java
peek / peekFirst / peekLast
Reads an end without removing it.
public E peekFirst(); public E peekLast(); E peek() // == peekFirst
Complexity
| Best case | O(1) |
|---|---|
| Worst case | O(1) |
| Extra space | O(1) |
One array read at the head or tail index.
Tested example
Deque<Integer> deque = new ArrayDeque<>(List.of(7, 8, 9));
assert deque.peekFirst() == 7; // reads, removes nothing
assert deque.peekLast() == 9;
assert deque.size() == 3;
assert new ArrayDeque<Integer>().peek() == null;
Behaviour
- Returns
- The element, or null when empty (getFirst/getLast throw instead).
- Errors and preconditions
- None for the peek forms.
- Changes
- Nothing.
- Memory and invalidation
- No allocation.