In Java, removeLast() removes the tail (last element) of an ArrayDeque and returns it. The element before it becomes the new tail.
If the deque is empty, it throws NoSuchElementException. To get null instead, use pollLast(). There is no queue- or stack-style alias for removeLast.
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, removeLast is also declared by the SequencedCollection interface, which Deque extends.
Syntax
Call removeLast with no arguments.
Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b", "c"));
String last = deque.removeLast(); // "c", deque = [a, b]
Call
What it does
deque.removeLast()
Removes and returns the tail; throws if empty.
deque.pollLast()
Same, but returns null if empty.
deque.getLast()
Returns the tail without removing it.
deque.removeFirst()
The same operation at the other end.
addLast plus removeLast also makes a stack, with the top at the tail. That is fine, but the standard push/pop pair works at the head.
Library declarations
Java
// java.util.ArrayDeque<E>
public E removeLast()
// Declared in Deque<E> (and SequencedCollection<E> since Java 21)
Parameters
Parameter
Description
None
removeLast() takes no parameters.
Return Value
Return Value
Description
E
The element that was at the tail. 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 removeLast().