The following is part of the List interface (which ArrayList implements):

E e = list.get(list.size() - 1);

E is the element type. If the list is empty, get throws an IndexOutOfBoundsException. You can find the whole API documentation here.

Answer from Johannes Schaub - litb on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_linkedlist_getlast.asp
Java LinkedList getLast() Method
abs() acos() addExact() asin() atan() atan2() cbrt() ceil() copySign() cos() cosh() decrementExact() exp() expm1() floor() floorDiv() floorMod() getExponent() hypot() IEEEremainder() incrementExact() log() log10() log1p() max() min() multiplyExact() negateExact() nextAfter() nextDown() nextUp() pow() random() rint() round() scalb() signum() sin() sinh() sqrt() subtractExact() tan() tanh() toDegrees() toIntExact() toRadians() ulp() Java Output Methods ... add() addAll() clear() clone() contains ensureCapacity() forEach() get() indexOf() isEmpty() iterator() lastIndexOf() listIterator() remove() removeAll() removeIf() replaceAll() retainAll() set() size() sort() spliterator() subList() toArray() trimToSize() Java LinkedList Methods
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ find-first-and-last-element-of-arraylist-in-java
Find first and last element of ArrayList in java - GeeksforGeeks
July 11, 2025 - Get the last element of ArrayList with use of get(index) method by passing index = size - 1. Below is the implementation of the above approach: ... // Java code to find first and last element // of ArrayList import java.util.ArrayList; public ...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ get the last element of a list
Java โ€“ Get the last element of a list | mkyong.com
October 15, 2019 - In Java, index starts at 0, we can get the last index of a list via this formula: list.size() - 1 ยท Python has reverse indexing, where -1 refer to the last element of a list, hope Java can implement it in the future release, then we can simplify the code like this:
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ get last value of a list in java
Get last value of a List in Java | Techie Delight
July 7, 2026 - This post will discuss how to get the last value of a List in Java... To retrieve the last element, you can use the expression `L.get(L.size() - 1)` where `L` is your list.
๐ŸŒ
Level Up Lunch
leveluplunch.com โ€บ java โ€บ examples โ€บ get-last-element-in-list
Get last element in list | Level Up Lunch
October 16, 2014 - @Test public void get_last_element_in_list_with_java () { List<String> strings = new ArrayList<String>(); strings.add("one"); strings.add("two"); strings.add("three"); String lastElement = null; if (!strings.isEmpty()) { lastElement = strings.get(strings.size() - 1); } assertEquals("three", lastElement); }
Find elsewhere
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2014 โ€บ 10 โ€บ how-to-get-the-last-element-of-arraylist
How to get the last element of Arraylist?
September 11, 2022 - There are times when we need to get the last element of an ArrayList, this gets difficult when we donโ€™t know the last index of the list. In this tutorial we are going to see an example to get the last element from ArrayList. import java.util.ArrayList; import java.util.List; public class ArrayListExample { public static void main(String[] args) { /* Creating ArrayList of Strings and adding * elements to it */ List<String> al = new ArrayList<String>(); al.add("Ajay"); al.add("Becky"); al.add("Chaitanya"); al.add("Dimple"); al.add("Rock"); // Displaying ArrayList elements System.out.println("ArrayList contains: "+al); // Logic to get the last element from ArrayList if (al != null && !al.isEmpty()) { System.out.println("Last element is:"); System.out.println(al.get(al.size()-1)); } } }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-find-the-last-occurrence-of-an-element-in-a-java-list
How to find the last occurrence of an element in a Java List?
June 10, 2025 - The Stream API was introduced in Java 8 provides a functional approach to working with collections. We can use the filter() method to filter the elements in the list and then use the reduce() method to find the last occurrence of an element.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-get-first-and-last-elements-from-arraylist-in-java
How to get first and last elements from ArrayList in Java?
September 1, 2025 - Following is the Java program to get the first and last elements from an ArrayList using the for loop: import java.util.ArrayList; public class GetTheFirstandLastEl{ public static void main(String[] args){ ArrayList<String> list = new ArrayList<>(); list.add("Banana"); list.add("Apple"); list.add("Mango"); list.add("Orange"); list.add("Grapes"); list.add("Pineapple"); String firstElement = ""; String lastElement = ""; for(int i=0; i<list.size(); i++){ if(i == 0){ firstElement = list.get(i); } if(i == list.size()-1){ lastElement = list.get(i); } } System.out.println("First element: " + firstElement); } }
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ java arraylist get last element
How to Get the Last Element From an ArrayList in Java | Delft Stack
February 12, 2024 - Utilizing the get() method with a direct index to retrieve the last element of an ArrayList is favored for its simplicity and clarity. By directly specifying the index (list.size() - 1), it offers a concise and easy-to-understand solution.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ linkedlist-getlast-method-in-java
LinkedList getLast() Method in Java - GeeksforGeeks
July 11, 2025 - Return type: It return the last element of the list. Example 2: Here, the getLast() method throws an NoSuchElementException if the list is empty. ... // Throws NoSuchElementException import java.util.LinkedList; class Geeks { public static void ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ get-first-and-last-elements-from-arraylist-in-java
Get first and last elements from ArrayList in Java - GeeksforGeeks
July 11, 2025 - Use get(size() - 1) to access the last element. Always check whether the list is null or empty before accessing its elements.
๐ŸŒ
Vultr
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ util โ€บ ArrayList โ€บ lastIndexOf
Java ArrayList lastIndexOf() - Find Last Index | Vultr Docs
November 15, 2024 - The lastIndexOf() method in Java's ArrayList class is a useful tool for determining the last occurrence of a specified element in a list. This method returns the index of the last occurrence of the specified element, or -1 if the element is ...
๐ŸŒ
Java Guides
javaguides.net โ€บ 2024 โ€บ 02 โ€บ java-8-program-to-retrieve-last-element-of-list-of-strings.html
Java 8 Program to Retrieve Last Element of a List of Strings
March 12, 2024 - 2. A list named strings is created and initialized with an array of string values. 3. To find the last element, the program converts the list into a stream using the stream() method.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-get-last-element-in-array-in-java
How to Get Last Element in Array in Java? - GeeksforGeeks
July 23, 2025 - In Java, to get the last element in an array, we can access the element at the index array.length - 1 using array indexing.
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ how to get the last value of an arraylist
How to get the last value of an ArrayList | Codemia
January 25, 2025 - You can safeguard your code by checking if list.size() > 0 before accessing the last element. Code Clarity: Choose the method that makes the code more readable and understandable within its context. For example, the Stream API might be preferred in an application that widely uses functional programming features. Retrieving the last element of an ArrayList can be done in multiple ways, each with its benefits and appropriate contexts. Understanding these methods allows Java developers to write more efficient, readable, and robust applications.
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ get-the-last-element-of-a-java-collection
How to Get the Last Element of a Java Collection: Fast & Straightforward Methods Explained โ€” javathinking.com
Use size() to get the number of elements, then get(index) with index = size() - 1. ArrayList uses an array under the hood, enabling O(1) (constant-time) access to any index, including the last element. import java.util.ArrayList; import ...