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)
Call
What 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
Parameter
Description
None
removeLast() takes no parameters.
Return Value
Return Value
Description
E
The element that was removed from the end (may be null).
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);
}
}
Output
undo bold
undo type i
history: [type H]
How It Works
Record edits. Each action is appended, so the most recent edit is always the last element.
Undo the latest.removeLast() returns "bold", the last action, and removes it.
Then the one before. The second undo removes "type i".
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));
}
}
}
Output
removeLast: NoSuchElementException
remove(size() - 1) asked for index -1
How It Works
Clear failure.removeLast() on an empty list throws NoSuchElementException, which simply says "there is no element".
Hidden index.empty.size() - 1 evaluates to -1, an index that can never exist.
Index failure.remove(-1) throws an IndexOutOfBoundsException (older JDKs throw its subclass ArrayIndexOutOfBoundsException), and the catch block handles both.
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);
}
}
Output
dropped 2 blank lines: [title, body]
How It Works
Two blank lines at the end. The last two strings are empty ("").
Check, then remove. The loop reads the last line with getLast() and removes it only while it is blank.
Short-circuit guard.!lines.isEmpty() is tested first, so getLast() is never called on an empty list, even if every line were blank.
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.