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
🌐
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 ...
🌐
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 - Then get(list.size()-1) method to get the last element of the ArrayList. Following is the Java program to get the first and last elements from an ArrayList:
🌐
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 - To retrieve the last element efficiently, the ArrayList fruits is then transformed into an ArrayDeque named dequeFruits. The last element is obtained using the getLast() method of the ArrayDeque.
🌐
BeginnersBook
beginnersbook.com › 2014 › 10 › how-to-get-the-last-element-of-arraylist
How to get the last element of Arraylist?
September 11, 2022 - 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 › get-first-and-last-elements-from-arraylist-in-java
Get first and last elements from ArrayList in Java - GeeksforGeeks
July 11, 2025 - The get() method is the simplest way to access elements from an ArrayList. Since the first element is present at index 0, we can use get(0) to retrieve it.
🌐
JavaGoal
javagoal.com › home › getlast() method
getlast java - getlast() arraylist to get last element of list
June 6, 2021 - import java.util.LinkedList; public class ExampleOfLinkedList { public static void main(String[] args) { LinkedList<String> listOfNames = new LinkedList<String>(); listOfNames.add("JAVA"); listOfNames.add("GOAL"); listOfNames.add("RAVI"); // It returns the element present at index 1 System.out.println("Element present at last position is = "+listOfNames.getLast()); } }
🌐
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> ...
🌐
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.
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_linkedlist_getlast.asp
Java LinkedList getLast() Method
compare() equals() sort() fill() length Java ArrayList 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 · add() addAll() clear() clone() contains forEach() get() getFirst() getLast() indexOf() isEmpty() iterator() lastIndexOf() listIterator() remove() removeAll() removeFirst() removeIf() removeLast() replaceAll() retainAll() set() size() sort() spliterator() subList() toArray() Java HashMap Methods ·
🌐
W3docs
w3docs.com › home › code snippets › java › how to get the last value of an arraylist
How to get the last value of an ArrayList | W3docs
import java.util.ArrayList; ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); if (!list.isEmpty()) { int lastValue = list.get(list.size() - 1); // lastValue will be 3 }
🌐
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_ele... lastElement); } Guava Iterables.getLast will return the last element in the iterable or the default value if the list is empty....
🌐
Java67
java67.com › 2015 › 06 › how-to-get-first-and-last-elements-form-ArrayList-java.html
How to get first and last elements form ArrayList in Java | Java67
That's all on how to get the first and last element from ArrayList in Java. Java Collection API does provide method to access ArrayList elements by index, these methods are actually defined inside the List interface.
🌐
W3Schools
w3schools.com › java › ref_arraylist_lastindexof.asp
Java ArrayList lastIndexOf() Method
compare() equals() sort() fill() length Java ArrayList 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 · add() addAll() clear() clone() contains forEach() get() getFirst() getLast() indexOf() isEmpty() iterator() lastIndexOf() listIterator() remove() removeAll() removeFirst() removeIf() removeLast() replaceAll() retainAll() set() size() sort() spliterator() subList() toArray() Java HashMap Methods ·
🌐
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 - For those using Java 8 and later, the Stream API provides a more modern approach to handling collections, including finding the last element.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › java arraylist.lastindexof()
Java Arraylist.lastIndexOf() with Examples - HowToDoInJava
January 13, 2023 - Learn to get the index of last occurrence of an element in the arraylist in Java using Arraylist.lastIndexOf() method with a simple example.
🌐
Ramesh Fadatare
rameshfadatare.com › home › java arraylist getlast() method (introduced in java 21)
Java ArrayList getLast() Method (introduced in Java 21)
June 12, 2024 - The getLast method can be used to retrieve the last element of the ArrayList. import java.util.ArrayList; import java.util.List; public class GetLastExample { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Orange"); ...
🌐
JetBrains
youtrack.jetbrains.com › issue › IDEA-345053 › Faulty-suggestion-Can-be-replaced-with-getLast-call-for-ArrayList-on-Java-17
"Can be replaced with 'getLast()' call" for ArrayList on Java 17
Our website uses some cookies and records your IP address for the purposes of accessibility, security, and managing your access to the telecommunication network. You can disable data collection and cookies by changing your browser settings, but it may affect how this website functions.
🌐
Coderanch
coderanch.com › t › 729652 › java › element-ArrayList-QuakeEntry
How do I get the last element of an ArrayList<QuakeEntry> ? (Beginning Java forum at Coderanch)
April 25, 2020 - If the ArrayList in has N elements in it, this method should call onePassBubbleSort N - 1 times to sort the elements in in. Add code to sortByMagnitudeWithBubbleSort to print all the quakes before a pass, and then to print all the quakes after each pass, identifying the pass.
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › lastIndexOf
Java ArrayList lastIndexOf() - Find Last Index | Vultr Docs
November 15, 2024 - Create an ArrayList with some duplicate elements. Utilize the lastIndexOf() method to find the last occurrence of an element. ... import java.util.ArrayList; ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); ...