Java ArrayList removeLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 21+

Definition

In Java 21 and later, removeLast() removes the last element of an ArrayList and returns it. No other element moves, so it is fast.

It does the same job as remove(list.size() - 1), except that it throws NoSuchElementException on an empty list. Together with addLast() it lets an ArrayList act as a simple stack: push with addLast, pop with removeLast.

An instance method of java.util.ArrayList, added in Java 21. It was added to List through the new SequencedCollection interface, and ArrayList overrides it.

Java 21+ only.

removeLast() comes from the SequencedCollection interface added in Java 21 (JEP 431). The examples on this page need JDK 21 or newer; on Java 8, 11, or 17 the call fails to compile with "cannot find symbol". On older versions, use remove(list.size() - 1) after checking !list.isEmpty().

Syntax

Call removeLast with no arguments (Java 21+).

Java
// Java 21+
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
String last = list.removeLast();               // "c", list = [a, b]
String prev = list.remove(list.size() - 1);    // "b", list = [a]  (any version)
CallWhat it does
list.removeLast()Java 21+: removes and returns the last element.
list.remove(list.size() - 1)The same on all versions.
list.getLast()Reads the last element without removing it.
list.addLast(e)Java 21+: the matching "push" for stack-style use.

Removing the last element does not shift anything, so it runs in constant time.

Library declarations
Java
// java.util.ArrayList<E>  (since Java 21)
public E removeLast()

// Declared in SequencedCollection<E> and List<E> (Java 21)

Parameters

ParameterDescription
NoneremoveLast() takes no parameters.

Return Value

Return ValueDescription
EThe element that was removed from the end (may be null).

Compare with Similar Methods

Removing from the end, and related methods:

MethodHow it differs
remove(size() - 1)The classic form; works on every Java version.
removeFirst()Java 21+: removes index 0 and shifts the rest.
getLast()Java 21+: reads the last element and keeps it.
ArrayDeque.pop()A dedicated stack: push and pop work at the head.

Java Examples

Example 1 — An Undo Stack

Record edits with addLast and undo them with removeLast.

Java
import java.util.ArrayList;
import java.util.List;

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

        System.out.println("undo " + edits.removeLast());
        System.out.println("undo " + edits.removeLast());
        System.out.println("history: " + edits);
    }
}

How It Works

  1. Record edits. Each action is appended, so the most recent edit is always the last element.
  2. Undo the latest. removeLast() returns "bold", the last action, and removes it.
  3. Then the one before. The second undo removes "type i".
  4. Last in, first out. Only the first edit, "type H", is left in the history.

Example 2 — removeLast() vs remove(size() - 1) on an Empty List

Both fail on an empty list, but the index version hides a negative index.

Java
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

public class ArrayListRemoveLastEmpty {
    public static void main(String[] args) {
        List<String> empty = new ArrayList<>();

        try {
            empty.removeLast();
        } catch (NoSuchElementException e) {
            System.out.println("removeLast: " + e.getClass().getSimpleName());
        }
        try {
            empty.remove(empty.size() - 1);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("remove(size() - 1) asked for index " + (empty.size() - 1));
        }
    }
}

How It Works

  1. Clear failure. removeLast() on an empty list throws NoSuchElementException, which simply says "there is no element".
  2. Hidden index. empty.size() - 1 evaluates to -1, an index that can never exist.
  3. Index failure. remove(-1) throws an IndexOutOfBoundsException (older JDKs throw its subclass ArrayIndexOutOfBoundsException), and the catch block handles both.
  4. The printout. The message shows the computed index, -1, to make the bug visible.

Example 3 — Remove Trailing Empty Lines

Remove empty strings from the end of a list until real content is reached.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListRemoveLastTrailing {
    public static void main(String[] args) {
        List<String> lines = new ArrayList<>(Arrays.asList("title", "body", "", ""));

        int dropped = 0;
        while (!lines.isEmpty() && lines.getLast().isEmpty()) {
            lines.removeLast();
            dropped++;
        }
        System.out.println("dropped " + dropped + " blank lines: " + lines);
    }
}

How It Works

  1. Two blank lines at the end. The last two strings are empty ("").
  2. Check, then remove. The loop reads the last line with getLast() and removes it only while it is blank.
  3. Short-circuit guard. !lines.isEmpty() is tested first, so getLast() is never called on an empty list, even if every line were blank.
  4. Stop at real content. "body" is not empty, so the loop ends after dropping two lines.

Frequently Asked Questions

Java 21, through the SequencedCollection interface.
Use list.remove(list.size() - 1) after checking that the list is not empty.
Yes. Nothing has to shift, so it runs in constant time.
NoSuchElementException.
Yes: addLast() (or add()) to push and removeLast() to pop. ArrayDeque with push() and pop() is the more common choice.

Next: replaceAll()

Replace every element using a function (Java 8+).

replaceAll() 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