HOWTO · Java
How to Get the Last Element From an ArrayList in Java
Learn how to get the last element from a Java ArrayList, handle empty lists, and choose alternatives for repeated end access.
On this page
To get the last element of a Java ArrayList, read the element at list.get(list.size() - 1). Java uses zero-based indexes, so the last valid index is one less than the list’s size. Unlike Python lists, Java lists do not support negative indexes such as -1 for the final element.
Get the Last Element with size() and get()
Call size() to find how many elements the list contains, subtract one to calculate the last index, and pass that index to get(). For example, a four-element list has valid indexes 0 through 3, so its last element is at index 4 - 1.
Run this example:
import java.util.ArrayList;
public class LastElement {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
int last = numbers.get(numbers.size() - 1);
System.out.println(last);
}
}
40
This direct lookup is the usual choice when the value is an ArrayList and you need its final element. It does not require a stream, a copied sublist, or a conversion to another collection.
Handle an Empty ArrayList
An empty list has a size of zero, so size() - 1 is -1. Passing that index to get() throws IndexOutOfBoundsException. Check the list before accessing it, and choose a result that fits the caller’s needs.
For a method that represents “no element” with null, make that contract explicit. This helper also rejects a null list rather than treating it as empty:
import java.util.ArrayList;
public class LastElement {
static <T> T lastOrNull(ArrayList<T> list) {
if (list == null) {
throw new IllegalArgumentException("list must not be null");
}
return list.isEmpty() ? null : list.get(list.size() - 1);
}
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
String last = lastOrNull(names);
System.out.println(last == null ? "No elements" : last);
}
}
No elements
Returning null is simple, but callers must distinguish “no element” from a legitimate null value in a list. If that distinction matters, return an Optional<T> instead, or let the caller handle an empty list before calling get(). Do not catch IndexOutOfBoundsException as a substitute for deciding what empty input means.
Choose a Collection for Repeated End Access
For one lookup, converting an ArrayList to ArrayDeque or LinkedList adds allocation and copies the elements without helping the original list. Keep the ArrayList and use its last index.
If the program repeatedly adds and removes elements at both ends, choose a deque for that access pattern from the start. ArrayDeque provides end operations such as addLast() and getLast(); LinkedList also provides getLast(), but has different storage and memory characteristics. These types are alternatives to consider when designing the collection, not shortcuts for a single read. ArrayDeque does not allow null elements, so account for that if null values are part of the data model.
Use Guava’s Iterables.getLast()
If the project already depends on Guava, Iterables.getLast(iterable) is an optional alternative for an Iterable. The official Guava API documentation for Iterables.getLast() describes both overloads: the one-argument method throws NoSuchElementException when the iterable is empty, while Iterables.getLast(iterable, defaultValue) returns the provided default when there are no elements.
This option requires Guava on the project’s classpath; it is not part of the Java standard library. For an ArrayList with no other need for Guava, size() and get() keep the code dependency-free.
Summary
Use list.get(list.size() - 1) to retrieve the last element of a nonempty ArrayList. Check emptiness first and make the no-element policy clear, since an empty list produces index -1 and get() throws IndexOutOfBoundsException. Avoid converting the list for one access; consider ArrayDeque or another collection only when the broader pattern needs repeated operations at the ends. Guava’s Iterables.getLast() is available when that dependency is already appropriate.