Java ArrayDeque removeLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

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

ParameterDescription
NoneremoveLast() takes no parameters.

Return Value

Return ValueDescription
EThe 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 returns null or false instead. The highlighted cell matches removeLast().

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 last letter off a deque.

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

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

        String last = d.removeLast();

        System.out.println("removed " + last);
        System.out.println("left " + d);
        System.out.println("tail now " + d.getLast());
    }
}

How It Works

  1. Tail is c. The deque holds a, b, c, so c is last.
  2. Remove from the end. removeLast() returns c and the head is not touched.
  3. What is left. The deque prints [a, b].
  4. New tail. getLast() confirms that b is now the last element.

Example 2 — Trim a Ranking to the Top Three

The scores are sorted from high to low, so removing from the tail cuts the lowest scores.

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

public class ArrayDequeTopThree {
    public static void main(String[] args) {
        Deque<Integer> scores = new ArrayDeque<>(Arrays.asList(98, 91, 85, 77, 60));

        while (scores.size() > 3) {
            System.out.println("cut " + scores.removeLast());
        }
        System.out.println("top 3: " + scores);
    }
}

How It Works

  1. Sorted input. The best score, 98, is at the head and the lowest, 60, is at the tail.
  2. Too many scores. With five elements, the loop condition size() > 3 is true.
  3. Cut from the bottom. removeLast() removes 60, then 77; after that the size is 3 and the loop stops.
  4. Safe loop. The size check guarantees the deque is never empty when removeLast() runs, so no exception is possible.

Example 3 — Simulate the Backspace Key

Each typed character is added at the tail, and < stands for backspace, which removes the last character.

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

public class ArrayDequeBackspace {
    public static void main(String[] args) {
        String keys = "cat<<ow";
        Deque<Character> typed = new ArrayDeque<>();

        for (char k : keys.toCharArray()) {
            if (k == '<') {
                if (!typed.isEmpty()) {
                    typed.removeLast();
                }
            } else {
                typed.addLast(k);
            }
        }

        StringBuilder sb = new StringBuilder();
        for (char c : typed) {
            sb.append(c);
        }
        System.out.println(typed);
        System.out.println(sb);
    }
}

How It Works

  1. Type c, a, t. Three characters are added at the tail: [c, a, t].
  2. Two backspaces. Each < calls removeLast(), removing t and then a.
  3. Type o, w. They are added after c, giving [c, o, w]. The isEmpty() check protects against a backspace on empty text.
  4. Join the characters. The enhanced for loop unboxes each Character to a char, and the builder produces cow.

Frequently Asked Questions

It removes and returns the last element (tail) of the deque.
removeLast() throws NoSuchElementException. pollLast() returns null instead.
getLast() only reads the tail. removeLast() reads it and removes it.
No. pop() and remove() work at the head, so they correspond to removeFirst(), not removeLast().
Yes, it runs in constant time on an ArrayDeque.

Next: removeLastOccurrence()

Remove the last element equal to a value.

removeLastOccurrence() 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.

10 people found this page helpful