Java ArrayDeque pollLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

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]
CallWhat 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

ParameterDescription
NonepollLast() takes no parameters.

Return Value

Return ValueDescription
EThe former tail element.
nullIf 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 returns null or false instead. The highlighted cell matches pollLast().

OperationHead: throwsHead: special valueTail: throwsTail: special value
InsertaddFirst()offerFirst()addLast()offerLast()
RemoveremoveFirst()pollFirst()removeLast()pollLast()
ExaminegetFirst()peekFirst()getLast()peekLast()

Queue and stack names: add() = addLast(), offer() = offerLast(), push() = addFirst(), pop() = removeFirst(), remove() = removeFirst(), poll() = pollFirst(), element() = getFirst(), peek() = peekFirst().

Java Examples

Example 1 — Remove the Last Element

Take the tail off a three-element deque.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequePollLastDemo {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>(Arrays.asList("a", "b", "c"));

        String last = d.pollLast();

        System.out.println("removed " + last);
        System.out.println("left " + d);
        System.out.println("new last " + d.peekLast());
    }
}

How It Works

  1. Three elements. c is at the tail.
  2. pollLast removes it. The call returns c and the deque shrinks to [a, b].
  3. Head untouched. a is still first; only the tail end changed.
  4. New tail. peekLast() now returns b.

Example 2 — An Undo History at the Tail

Edits are recorded with addLast, and undo takes the most recent one back with pollLast.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeUndo {
    public static void main(String[] args) {
        Deque<String> edits = new ArrayDeque<>();
        edits.addLast("type H");
        edits.addLast("type i");
        edits.addLast("bold");

        System.out.println("undo " + edits.pollLast());
        System.out.println("undo " + edits.pollLast());
        System.out.println("remaining " + edits);
    }
}

How It Works

  1. Record edits. Each edit is added at the tail, so the newest edit, bold, is last.
  2. Undo the newest. The first pollLast() returns bold.
  3. Undo again. The second call returns type i, the edit before it.
  4. Oldest stays. Only type H is left. Adding and polling at the same end gives last-in, first-out order.

Example 3 — Keep the Three Most Recent Files

New files go to the head; when there are more than three, the oldest is dropped from the tail.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeRecentFiles {
    public static void main(String[] args) {
        Deque<String> recent = new ArrayDeque<>();
        String[] opened = {"a.txt", "b.txt", "c.txt", "d.txt"};

        for (String f : opened) {
            recent.offerFirst(f);
            if (recent.size() > 3) {
                System.out.println("dropped " + recent.pollLast());
            }
        }
        System.out.println(recent);
        System.out.println(new ArrayDeque<String>().pollLast());
    }
}

How It Works

  1. Newest first. offerFirst puts each opened file at the head, so the list reads newest to oldest.
  2. A fourth file. After d.txt the size is 4, so pollLast() drops a.txt, the oldest file.
  3. The final list. Three files remain, newest first: [d.txt, c.txt, b.txt].
  4. A brand-new deque. Calling pollLast() on an empty ArrayDeque prints null instead of throwing.

Frequently Asked Questions

It removes and returns the last element (tail), or returns null if the deque is empty.
Both remove the tail. pollLast() returns null on an empty deque; removeLast() throws NoSuchElementException.
No, unless the deque had only one element, in which case it becomes empty.
Yes. Adding and removing at the same end gives last-in, first-out order. push() and pop() do the same at the head.
Constant time on an ArrayDeque.

Next: pop()

Stack-style removal of the head; throws if empty.

pop() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful