Java ArrayList getLast() Method

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

Definition

In Java 21 and later, getLast() returns the last element of an ArrayList, the one at index size() - 1, without removing it.

Before Java 21 you had to write list.get(list.size() - 1), which is easy to get wrong by one. getLast() reads the same element, throws NoSuchElementException on an empty list, and matches getLast() on Deque and LinkedList.

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.

getLast() 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 get(list.size() - 1) after checking !list.isEmpty().

Syntax

Call getLast with no arguments (Java 21+).

Java
// Java 21+
List<Integer> list = new ArrayList<>(Arrays.asList(10, 20, 30));
int last = list.getLast();               // 30, list unchanged
int same = list.get(list.size() - 1);    // 30 on every Java version
CallWhat it does
list.getLast()Java 21+: the last element; throws if empty.
list.get(list.size() - 1)Same element on all versions.
list.get(list.size())Off-by-one bug: always IndexOutOfBoundsException.
list.removeLast()Java 21+: reads and removes the last element.

getLast() is a constant-time read; it does not walk the list and does not change it.

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

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

Parameters

ParameterDescription
NonegetLast() takes no parameters.

Return Value

Return ValueDescription
EThe last element. Can be null if a null is stored there.

Compare with Similar Methods

Ways to read the end of a list:

MethodHow it differs
get(size() - 1)The classic form; works on every Java version.
getFirst()Java 21+: reads index 0 instead.
removeLast()Java 21+: also removes the element.
ArrayDeque.peekLast()Returns null on an empty deque instead of throwing.

Java Examples

Example 1 — Read the Latest Temperature

Readings are appended in time order, so the newest is the last element.

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

public class ArrayListGetLastDemo {
    public static void main(String[] args) {
        List<Double> temps = new ArrayList<>();
        temps.add(21.5);
        temps.add(22.0);
        temps.add(23.25);

        System.out.println("latest: " + temps.getLast());
        System.out.println("classic: " + temps.get(temps.size() - 1));
        System.out.println("readings: " + temps.size());
    }
}

How It Works

  1. Readings in time order. Each add appends a boxed Double, so the newest temperature is always at the end.
  2. Newest value. getLast() returns 23.25, the reading added last.
  3. Old idiom. get(temps.size() - 1) computes index 2 and returns the same value.
  4. Unchanged. Neither call removes anything; there are still 3 readings.

Example 2 — The size() Off-by-One Bug

Asking for index size() is one past the end; size() - 1 or getLast() is correct.

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

public class ArrayListGetLastOffByOne {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>(Arrays.asList("alpha", "beta", "gamma"));

        try {
            System.out.println(words.get(words.size()));
        } catch (IndexOutOfBoundsException e) {
            System.out.println("get(size()) is one past the end");
        }
        System.out.println("get(size() - 1): " + words.get(words.size() - 1));
        System.out.println("getLast(): " + words.getLast());
    }
}

How It Works

  1. Size is 3. The indexes are 0, 1, and 2, so 3 is already past the end.
  2. A common bug. words.get(words.size()) asks for index 3 and throws before anything is printed by that line.
  3. The fix. Subtracting 1 gives index 2, which holds "gamma".
  4. No arithmetic. getLast() avoids the index math altogether and returns the same word.

Example 3 — Guard getLast() on a List That Might Be Empty

A helper returns "none" for an empty list; the unguarded call shows the exception.

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

public class ArrayListGetLastGuard {
    static String lastOrNone(List<String> list) {
        return list.isEmpty() ? "none" : list.getLast();
    }

    public static void main(String[] args) {
        List<String> orders = new ArrayList<>();
        System.out.println("last order: " + lastOrNone(orders));

        try {
            orders.getLast();
        } catch (NoSuchElementException e) {
            System.out.println("unguarded: " + e.getClass().getSimpleName());
        }

        orders.add("#1001");
        orders.add("#1002");
        System.out.println("last order: " + lastOrNone(orders));
    }
}

How It Works

  1. Helper method. lastOrNone returns "none" for an empty list and otherwise calls getLast().
  2. Empty case. With no orders yet, the helper skips the call and prints none.
  3. Without the guard. Calling getLast() directly on the same empty list throws NoSuchElementException.
  4. After two orders. The helper now returns "#1002", the most recent order.

Frequently Asked Questions

Java 21, as part of the SequencedCollection interface.
Use list.get(list.size() - 1) after checking that the list is not empty.
NoSuchElementException.
No. It only reads it. removeLast() reads and removes it.
No, both are constant-time reads. getLast() is simply clearer and avoids off-by-one mistakes.

Next: indexOf()

Find the first index of a value, or -1.

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

9 people found this page helpful