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
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_lastindexof.asp
Java ArrayList lastIndexOf() Method
If the item is not found in the list then it returns -1. ... Coding fundamentals as a game. Bite-sized lessons and challenges. ... Ready to start your journey? Your streak is waiting. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Discussions

java - How to get the last value of an ArrayList - Stack Overflow
Starting with Java 21, the list's getLast method can be used. This returns the last item of the list, throwing a NoSuchElementException if the list is empty. ... This getLast method is inherited from a super-interface of List, SequencedCollection. ... Save this answer. ... Show activity on this post. I use micro-util class for getting last (and first) element ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to get the last value of an element in a List in Java? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
Get last X items of a list in java - Stack Overflow
I can only seem to find answers about last/first element in a list or that you can get a specific item etc. Lets say I have a list of 100 elements, and I want to return the last 40 elements. How ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to get the last element of list in tcl?
set foo {1 2 3} puts [lindex $foo end] More on reddit.com
๐ŸŒ r/Tcl
3
6
September 21, 2023
๐ŸŒ
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 - package com.mkyong.test; import java.util.Arrays; import java.util.List; public class JavaExample1 { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); System.out.println(list.get(list.size() - 1)); System.out.println(list.get(list.size() - 2)); System.out.println(list.get(list.size() - 3)); System.out.println(list.get(list.size() - 4)); System.out.println(list.get(list.size() - 5)); } } ... 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:
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ java-last-element-arraylist
How to get the Last Element of an ArrayList in Java - Sabe.io
April 18, 2022 - We can do this by subtracting 1 from the size of the list, since that should return us the last element. ... JAVAimport java.util.Arrays; import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> ...
Find elsewhere
๐ŸŒ
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); }
๐ŸŒ
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 - import java.util.ArrayList; import java.util.List; import java.util.OptionalInt; import java.util.stream.Collectors; import java.util.stream.IntStream; public class LastOccurrenceInList { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(3); int elementToFind = 3; OptionalInt lastIndex = IntStream.range(0, list.size()) .filter(i -> list.get(i).equals(elementToFind)) .reduce((first, second) -> second); System.out.println("Last occurrence of " + elementToFind + " is at index: " + lastIndex.orElse(-1)); } }
๐ŸŒ
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 - By employing the get() method with a direct index, we have successfully obtained the last element from the ArrayList. This technique offers a concise and readable solution for retrieving specific elements based on their position within the list.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ linkedlist-getlast-method-in-java
LinkedList getLast() Method in Java - GeeksforGeeks
July 11, 2025 - Example 1: Here, we use the getLast() method to retrieve the last element of the list. ... // Java Program to Demonstrate the // use of getLast() in LinkedList import java.util.LinkedList; public class Geeks { public static void main(String ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ...