In Java, getLast() returns the tail (last element) of an ArrayDeque without removing it. If the deque is empty, it throws NoSuchElementException.
Its "special value" partner is peekLast(), which returns null on an empty deque. In a queue, the tail is the element that was added most recently.
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. Since Java 21, getLast is also declared by the SequencedCollection interface, which Deque extends.
Syntax
Call getLast with no arguments.
Java
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(10, 20, 30));
int last = deque.getLast(); // 30, deque unchanged
Call
What it does
deque.getLast()
Returns the tail; throws if empty.
deque.peekLast()
Returns the tail, or null if empty.
deque.removeLast()
Returns the tail and removes it.
deque.getFirst()
Reads the other end.
There is no Queue or stack alias for getLast. Reading the tail is a constant-time operation.
Library declarations
Java
// java.util.ArrayDeque<E>
public E getLast()
// Declared in Deque<E> (and SequencedCollection<E> since Java 21)
Parameters
Parameter
Description
None
getLast() takes no parameters.
Return Value
Return Value
Description
E
The tail of the deque. Never null.
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 getLast().