In Java, peekLast() returns the tail (last element) of an ArrayDeque without removing it. If the deque is empty, it returns null.
It is the "special value" version of getLast(). Its mirror at the head is peekFirst().
An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.
Syntax
Call peekLast with no arguments.
Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("mon", "tue"));
String last = deque.peekLast(); // "tue", deque unchanged
Call
What it does
deque.peekLast()
Returns the tail, or null.
deque.getLast()
Returns the tail; throws if empty.
deque.pollLast()
Returns the tail and removes it.
deque.peekFirst()
Reads the head instead.
There is no Queue or stack alias for peekLast. It is handy whenever you need to compare a new element with the one added just before it.
Library declarations
Java
// java.util.ArrayDeque<E>
public E peekLast()
// Declared in Deque<E>
Parameters
Parameter
Description
None
peekLast() takes no parameters.
Return Value
Return Value
Description
E
The tail of the deque, left in place.
null
If the deque is empty.
Throw or Return a Special Value?
Each end has a method that throws when it cannot work (such as on an empty deque) and one that returnsnull or false instead. The highlighted cell matches peekLast().