Get last value of a List in Java
This post will discuss how to get the last value of a List in Java.
1. Using List.get() method
The size() method returns the total number of items present in the List. To retrieve the last element, you can use the expression L.get(L.size() - 1) where L is your list. Here’s the complete code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); Character lastItem = chars.get(chars.size() - 1); System.out.println(lastItem); // 'Z' } } |
This, however, throws an IndexOutOfBoundsException if the list is empty. To handle it, you can create a utility method with an additional size check on the list before calling its get() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static <T> T getLast(List<T> list) { if (list != null && !list.isEmpty()) { return list.get(list.size() - 1); } return null; } public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); Character lastItem = getLast(chars); System.out.println(lastItem); // 'Z' } } |
2. Using Guava
With Google Guava, you can use the Iterables.getLast() method, designed specifically to get the last element of an iterable.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.collect.Iterables; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); Character lastItem = Iterables.getLast(chars); System.out.println(lastItem); // 'Z' } } |
This method will throw a java.util.NoSuchElementException if the iterable is empty. However, you can provide a default value in the second parameter to the Iterables.getLast() method to avoid the exception.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.collect.Iterables; import java.util.List; class Main { public static void main(String[] args) { List<Integer> emptyList = List.of(); Integer lastItem = Iterables.getLast(emptyList, null); System.out.println(lastItem); // null } } |
3. Using Stream API
Here’s a solution using Stream API. The idea is to get a stream of all elements in the list, skip the first n-1 elements in it where n is the list’s size, and return the only element left in the stream.
This approach is demonstated below. It not recommended for Lists with RandomAccess support, as it takes linear time as opposed to O(1) time by above discussed methods.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static <T> T getLast(List<T> list) { return list.stream().skip(list.size() - 1).findFirst().orElse(null); } public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); Character lastItem = getLast(chars); System.out.println(lastItem); // 'Z' } } |
4. Using for loop
A naive approach will be to use a for-loop to iterate through the array and return the last element. This approach is not recommended as it is very inefficient.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static <T> T getLast(List<T> list) { T lastItem = null; for (T e : list) { lastItem = e; } return lastItem; } public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); Character lastItem = getLast(chars); System.out.println(lastItem); // 'Z' } } |
That’s all about getting the last value of a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)