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 ...
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › List.html
List (Java SE 21 & JDK 21)
January 20, 2026 - Gets the last element of this collection. ... If this List is not empty, the implementation in this interface returns the result of calling get(size() - 1).
🌐
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)); } } Output · 5 4 3 2 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: List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); System.out.println(list.get(-1)); // 5 System.out.println(list.get(-2)); // 4 System.out.println(list.get(-3)); // 3 System.out.println(list.get(-4)); // 2 System.out.println(list.get(-5)); // 1 ·
🌐
Baeldung
baeldung.com › home › java › java streams › how to get the last element of a stream in java?
How to Get the Last Element of a Stream in Java? | Baeldung
January 8, 2024 - Let’s create a List of string values and use its size function to determine how many elements to skip to reach the last element. Here is the example code getting the last element using skip:
🌐
Facebook
facebook.com › groups › jugbd › posts › 4989621114417867
How to get the last element of a list using Java Stream API
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › find-the-last-element-of-a-stream-in-java
Find the last element of a Stream in Java - GeeksforGeeks
July 12, 2025 - Below is the implementation of the above approach. The skip() method returns a stream after removing first N elements. Therefore this method can be used to skip the elements except the last one.
🌐
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 - It's about the last item you want to actually compare during the iteration. That is, it's really about which item you terminate the iteration with. That's what the hint refers to as "the last list item." Think about the algorithm: you're "bubbling" the largest element up to the end of the list, right?
🌐
W3Schools
w3schools.com › java › ref_linkedlist_getlast.asp
Java LinkedList getLast() Method
The getLast() method returns the last item in a list. Tip: Use the getFirst() method to get the first item in a list. ... T refers to the data type of items in the list.
🌐
Devtalk
forum.devtalk.com › backend developer forum › backend questions/help
How to get the first and last occurrence of the specified elements in a linked list in Java? - Backend Questions/Help - Devtalk
March 31, 2023 - Hello all, I am new to learning Java Programming and want to learn java from scratch. I was writing a Java program to get the first and last occurrence of the specified elements in a linked list. Here is the Code: import java.util.LinkedList; import java.util.Iterator; public class program { public static void main(String[] args) { // create an empty linked list LinkedList l_list = new LinkedList (); // use add() method to add values in the linked list l...
🌐
BeginnersBook
beginnersbook.com › 2014 › 10 › how-to-get-the-last-element-of-arraylist
How to get the last element of Arraylist?
September 11, 2022 - 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)); } } } Output: ArrayList contains: [Ajay, Becky, Chaitanya, Dimple, Rock] Last element is: Rock ·
🌐
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 ...
🌐
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 - Check if the current index is equal to the size of the ArrayList - 1, if it is, assign the current element to the last element variable. 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 developers to write more efficient, readable, and robust applications.
🌐
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.
🌐
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); }
🌐
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 }