In Java, pollLast() removes the tail (last element) of an ArrayDeque and returns it, or returns null if the deque is empty.
It is the "special value" twin of removeLast(), which throws instead. Its mirror at the head is pollFirst().
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 pollLast with no arguments.
Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b", "c"));
String last = deque.pollLast(); // "c", deque = [a, b]
Call
What it does
deque.pollLast()
Removes and returns the tail, or null.
deque.removeLast()
Same, but throws when empty.
deque.peekLast()
Reads the tail without removing it.
deque.pollFirst()
Removes from the head instead.
There is no Queue or stack alias for pollLast; you always call it by this name.
Library declarations
Java
// java.util.ArrayDeque<E>
public E pollLast()
// Declared in Deque<E>
Parameters
Parameter
Description
None
pollLast() takes no parameters.
Return Value
Return Value
Description
E
The former tail element.
null
If the deque was 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 pollLast().