How to get the last value of an ArrayList
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In Java, an ArrayList is a resizable array implementation of the List interface. It provides a convenient way to store and manipulate a dynamic list of objects. One common operation with an ArrayList is retrieving the last element, which can be achieved in several ways depending on the context and specific requirements. In this article, we'll explore the methods to obtain the last value of an ArrayList, considering various scenarios and their implications for performance and readability.
Methods to Retrieve the Last Element of an ArrayList
Using Simple Indexing
The most straightforward method to access the last element of an ArrayList is by using its size to calculate the index of the last element. Since ArrayList provides the size() method, which returns the number of elements in the list, and since ArrayList is zero-based index, the last element is at position size() - 1.
Example:
This will output Last Element: Cherry.
Using ListIterator
Another way to get the last element is using a ListIterator. This is particularly useful when you want to traverse the list in reverse order. The ListIterator can be obtained by calling the listIterator() method on the ArrayList.
Example:
This method is useful if you need more control over the list traversal or if you start the iteration not knowing if you will need the last element or not.
Using Stream API (Java 8+)
For those using Java 8 and later, the Stream API provides a more modern approach to handling collections, including finding the last element.
Example:
The reduce() method combines the elements of the stream to a single value; in this case, it always passes the second argument as the accumulator, effectively returning the last element.
Considerations and Best Practices
- Efficiency: The method of accessing via direct index (
list.get(list.size() - 1)) is generally the most efficient, given its constant-time complexity (). - Safety: Always ensure the list is not empty to avoid
IndexOutOfBoundsException. You can safeguard your code by checking iflist.size() > 0before 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.
Summary Table
| Method | Code Clarity | Performance | Use Case |
| Direct Indexing | High | Excellent | When performance is crucial, and the list is not modified frequently. |
| ListIterator | Medium | Good | Useful for reverse iteration or when index is unknown. |
| Stream API | High | Moderate | Preferred in functional programming or Java 8+ environments. |
Conclusion
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.
Related reading
- How to get the nth element of a python list or a default if not available
- how to get the one entry from hashmap without iterating
- How to get topic list from kafka server in Java
- How to get topics list from Kafka using C#
- How to get the name of a class without the package?
- How to get the number of threads in a Java process
- How to git-pull a given patch set from Gerrit?
- How to group by the elements of an array in Swift

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.