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
Java Examples Java Videos Java ... cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); // Use getLast() to get the last item in the list System.out.println(cars.getLast()); } }...
🌐
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 ... 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, ...
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
I'm trying to compare a String to see if it's equal to the last value stored in the element lastStatus inside the list. As the event is updated, many values are stored in said element. I'm trying to get the last one stored. 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
The easy way to access the last JavaScript array element
There was a competing proposal to add arr.lastElement and I think arr.lastIndex. But it was abandoned in favor of arr.at(). More on reddit.com
🌐 r/javascript
46
19
March 29, 2024
🌐
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)); } } }
🌐
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 ...
🌐
Level Up Lunch
leveluplunch.com β€Ί java β€Ί examples β€Ί get-last-element-in-list
Get last element in list | Level Up Lunch
October 16, 2014 - This snippet will get the last element in a ArrayList using straight up java. It will check if the ArrayList is empty and if the size is greater than 0 returning the last element in the collection. @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); }
🌐
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
🌐
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.
🌐
Reactgo
reactgo.com β€Ί home β€Ί how to get the last element of an arraylist in java
How to get the last element of an ArrayList in Java | Reactgo
October 12, 2023 - To get the last element of a ArrayList in Java, we can use the list.get() method by passing its index list.size()-1. Here is an example, that returns the last element 40 from the following ArrayList: import java.util.Arrays; import java.util.List; ...
🌐
JavaGoal
javagoal.com β€Ί home β€Ί getlast() method
getlast java - getlast() arraylist to get last element of list
June 6, 2021 - getLast() method: This getLast java method employs to get the last element from LinkedList. It returns the object. ... Where, E represents the type of elements in LinkedList. It throws NoSuchElementException if this LinkedList is empty. import java.util.LinkedList; public class ExampleOfLinkedList ...
🌐
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.
🌐
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); } }
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί java β€Ί linkedlist-getlast-method-in-java
LinkedList getLast() Method in Java - GeeksforGeeks
July 11, 2025 - // Java Program to Demonstrate ... l.add("Geeks"); System.out.println("" + l); // Use getLast() to display the // last element of the list System.out.println("" + l.getLast()); } } ... Return type: It return the last element ...
🌐
BeginnersBook
beginnersbook.com β€Ί 2014 β€Ί 07 β€Ί java-get-first-and-last-elements-from-linkedlist-example
Java – Get first and last elements from LinkedList example
September 11, 2022 - Here we have a LinkedList of String type and we are getting first and last element from it using getFirst() and getLast() methods of LinkedList class. Method definition and description are as follows: 1) public E getFirst(): Returns the first element in this list. 2) public E getLast(): Returns ...
🌐
HatchJS
hatchjs.com β€Ί home β€Ί java: how to get the last element of a list
Java: How to Get the Last Element of a List
January 5, 2024 - In this tutorial, we showed you how to get the last element of a list in Java. We discussed two methods for getting the last element of a list: the `.last()` method and the `.get(index)` method. We discussed the pros and cons of each method and provided examples of how to use each method.
🌐
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 ...
🌐
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 - The first element is present at index 0, while the last element is present at index size() - 1. Use get(0) to access the first element of an ArrayList. Use get(size() - 1) to access the last element. Always check whether the list is null or ...
🌐
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 - 3. To find the last element, the program converts the list into a stream using the stream() method. Then, the reduce() method is applied to the stream. The reduce() operation takes two elements at a time and applies the provided lambda expression, ...