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 scenario where you need first and last element of a list is supposed you have a sorted list and wants 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 its opposite then the first element would be the maximum and 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 size() method and not length, which is used to get the length of array. Earlier we have seen how to get first and last element from 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 to 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 code example to get both first and last elements from ArrayList in Java. It's pretty straight forward, 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 first and last element from the ArrayList in Java.

That's all on how to get first and last element from ArrayList in Java. Java Collection API does provides method to access ArrayList elements by index, these methods are actually defined inside List interface.

Further Learning
Java Fundamentals: Collections
From Collections to Streams in Java 8 Using Lambda Expressions
Grokking Algorithms by Aditya Bhargava
Java Programming Interview Exposed by Makham

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)

No comments:

Post a Comment