How to get first and last elements form ArrayList in Java

There are times when you need to get the first or last element of an ArrayList. One of the common scenarios where you need the first and last element of a list is supposed you have a sorted list and want to get the highest and lowest element? How do you get that? The first element is your lowest and the last element is your highest, provided ArrayList is sorted in ascending order. If it's opposite then the first element would be the maximum and the last element would be the minimum. This is quite easy to do in ArrayList because the first element is stored at index 0 and the last element is on index, size - 1. 

If you know how to get the size of ArrayList then you can easily get those two values. Just remember, that you need to use the size() method and not length, which is used to get the length of the array. Earlier we have seen how to get the first and last element from a linked list and In this tutorial, we are going to see an example of how to get the last element from ArrayList in Java.

By the way, if you are serious about learning Java collection framework in deep and want to master different types of collection classes e.g. List, Set, Map, ConcurrentMap, Queue, Stack, BlockingQueue, and other thread-safe collections introduced on Java 5 and 6, then I suggest you take a look at Java Generics and Collection by Maurice Naftalin and Philip Wadler, one of the best books to learn Java Collections.

First and Last elements from ArrayList in Java

Here is a code example to get both the first and last elements from ArrayList in Java. It's pretty straightforward, all you need to do is call get(0) to get the first element and call get(size() - 1) to retrieve the last element.

First and Last element of ArrayList in Java

In this example, we have an ArrayList of video games, some of the super hit titles of Nintendo era, first element is "Super Mario Bros" while last game is "Race". 

Let's see how we retrieved these two objects.

import java.util.Arrays;
import java.util.List;

 * Java Program to find first and last elements from Java ArrayList.
 * @author WINDOWS 8

public class FirstFromArrayList {
    public static void main(String args[]){
        // let's create ArrayList of String elements
        // Arrays.asList() is a shortcut to create and initialize
        // ArrayList in same line
        List<String> games = Arrays.asList("Super Mario Bros", 
             "MK3", "Brian Lara Cricket", "Donky Kong", "Race");
        // now let's find out first and last elements of ArrayList
        // first element is always on 0 index
        // last element is at size - 1 index
        String first = games.get(0);
        String last =  games.get(games.size() -1);
        System.out.println("ArrayList : " + games);
        System.out.println("First element in arraylist : " + first);
        System.out.println("Last element in arraylist : " + last);

ArrayList : [Super Mario Bros, MK3, Brian Lara Cricket, Donky Kong, Race]
First element in arraylist : Super Mario Bros
Last element in arraylist : Race

You can see that our program has correctly retrieved the first and last element from the ArrayList in Java.

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.

If you are interested to learn more about ArrayList, check out these interesting tutorials and interview questions :
  • How to sort ArrayList in descending order in Java? (answer)
  • How to synchronize ArrayList in Java? (answer)
  • Top 20 ArrayList Interview Questions for Java Beginners (list)
  • When to use ArrayList and LinkedList in Java? (answer)
  • Difference between ArrayList and HashSet in Java? (answer)
  • Difference between an array and ArrayList in Java? (answer)
  • How to remove elements from ArrayList in Java? (answer)
  • Difference between HashMap and ArrayList in Java? (answer)
  • What is difference between fail-safe and fail-fast Iterator? (answer)
  • Difference between length() of array and size() of ArrayList in Java? (answer)
  • How to convert ArrayList to String in Java? (answer)
  • How to remove duplicates from ArrayList in Java? (solution)
  • Difference between Vector and ArrayList in Java? (answer)
Now, is the quiz time, what is difference between a List and Set in Java? Can you pass a Set to a method which is expecting a List in Java?

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.