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
๐ŸŒ
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, ...
๐ŸŒ
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()); } } Try it Yourself ยป ยท...
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 - @Test public void get_last_element_in_list_with_guava () { List<String> strings = Lists.newArrayList("one", "two", "three"); String lastElement = Iterables.getLast(strings, null); 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 - This method is not only concise but also provides a clean and readable way to access elements, contributing to the maintainability of your Java code. Incorporating such techniques enhances your programming arsenal and promotes effective handling of ArrayLists in various applications. 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.
๐ŸŒ
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. E getLast(); Where, E represents the type of elements in LinkedList. It throws NoSuchElementException if this LinkedList is empty. import java.util.LinkedList; public class ...
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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); } }
๐ŸŒ
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 - import java.util.LinkedList; public ... Element is: "+firstElement); // Getting Last element of the List Object lastElement = linkedlist.getLast(); System.out.println("Last Element is: "+lastElement); } } Output: First Element is: Item1 Last Element is: Item6 ยท Java โ€“ Remove ...
๐ŸŒ
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 - A: There are several ways to get the last element of a list in Java. The simplest way is to use the `.get()` method, passing the index of the last element as an argument. For example, if you have a list of strings called `myList`, you can get the last element by using the following code:
๐ŸŒ
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, ...
๐ŸŒ
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 ...
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ java arraylist.lastindexof()
Java Arraylist.lastIndexOf() with Examples - HowToDoInJava
January 13, 2023 - ArrayList<String> list = new ...gary","alex","harry")); int lastIndex = list.lastIndexOf("alex"); System.out.println(lastIndex); lastIndex = list.lastIndexOf("hello"); System.out.println(lastIndex); Program output...