Get first and last elements from ArrayList in Java

Last Updated : 26 Aug, 2026

An ArrayList stores elements in an ordered sequence, where each element can be accessed using its index. The first element is present at index 0, while the last element is present at index size() - 1.

  • Use get(0) to access the first element of an ArrayList.
  • Use get(size() - 1) to access the last element.
  • Always check whether the list is null or empty before accessing its elements.

Example:

Input: List = [15, 25, 35, 45, 55]
Output: First element: 15
Last element: 55

Methods to Find First and Last Elements from ArrayList

The following methods can be used to find the first and last elements of an ArrayList :

1. Using get() Method

The get() method is the simplest way to access elements from an ArrayList. Since the first element is present at index 0, we can use get(0) to retrieve it. The last element can be accessed using get(size() - 1).

Approach

  1. Create an ArrayList and add elements to it.
  2. Check whether the list is null or empty.
  3. Use get(0) to get the first element.
  4. Use get(size() - 1) to get the last element.
  5. Print both elements.
Java
import java.util.ArrayList;
import java.util.List;

public class FindFirstLast{

    // Method to find first and last elements
    public static void getFirstLast(List<Integer> list)
    {

        // Check whether the list is not null and not empty
        if (list != null && !list.isEmpty()){

            // Get the first element
            System.out.println("First element: "+ list.get(0));

            // Get the last element
            System.out.println("Last element: "+ list.get(list.size() - 1));
        }
    }

    public static void main(String[] args)
    {

        List<Integer> list = new ArrayList<>();

        list.add(15);
        list.add(25);
        list.add(35);
        list.add(45);
        list.add(55);

        System.out.println("ArrayList: " + list);

        // Find first and last elements
        getFirstLast(list);
    }
}

Output
ArrayList: [15, 25, 35, 45, 55]
First element: 15
Last element: 55

Explanation: The get(0) method returns the element at the first index, which is 15. The size() method returns 5, so the last index is 5 - 1 = 4. Therefore, get(4) returns the last element, 55.

Note: get() is the preferred method for an ArrayList because accessing an element by index takes constant time.

2. Using Iterator

An Iterator can also be used to find the first and last elements of an ArrayList. The iterator starts from the first element and moves through the list until it reaches the last element.

Approach

  1. Create an ArrayList.
  2. Check whether the list is empty.
  3. Create an iterator using iterator().
  4. Use next() to get the first element.
  5. Traverse the remaining elements using hasNext().
  6. Store the last element during traversal.
  7. Print the first and last elements.
Java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class FindFirstLast {

    public static void getFirstLast(List<Integer> list)
    {

        if (list == null || list.isEmpty()){
            return;
        }

        Iterator<Integer> iterator = list.iterator();

        // Get the first element
        int first = iterator.next();

        int last = first;

        // Traverse the remaining elements
        while (iterator.hasNext()){
            last = iterator.next();
        }

        System.out.println("First element: " + first);
        System.out.println("Last element: " + last);
    }

    public static void main(String[] args){

        List<Integer> list = new ArrayList<>();

        list.add(12);
        list.add(24);
        list.add(36);
        list.add(48);
        list.add(60);

        System.out.println("ArrayList: " + list);

        getFirstLast(list);
    }
}

Output
ArrayList: [12, 24, 36, 48, 60]
First element: 12
Last element: 60

Explanation: The first call to next() gives the first element. The while loop then moves through the remaining elements, and the last value obtained from the iterator is stored as the last element.

Note: This approach requires traversing the list, so for an ArrayList, using get(0) and get(size() - 1) is simpler and more efficient.

3. Using Stream API

The Stream API can also be used to retrieve the first and last elements of an ArrayList. The findFirst() method directly returns the first element, while the last element can be obtained by processing the stream.

Approach

  1. Create an ArrayList.
  2. Convert the list into a stream.
  3. Use findFirst() to retrieve the first element.
  4. Use the stream to obtain the last element.
  5. Print the first and last elements.
Java
import java.util.ArrayList;
import java.util.List;

public class FindFirstLast{

    public static void main(String[] args){

        List<Integer> list = new ArrayList<>();

        list.add(11);
        list.add(22);
        list.add(33);
        list.add(44);
        list.add(55);

        System.out.println("ArrayList: " + list);

        if (!list.isEmpty()) {

            // Get the first element
            int first = list.stream()
                            .findFirst()
                            .get();

            // Get the last element
            int last = list.stream()
                           .reduce((a, b) -> b)
                           .get();

            System.out.println("First element: " + first);
            System.out.println("Last element: " + last);
        }
    }
}

Output
ArrayList: [11, 22, 33, 44, 55]
First element: 11
Last element: 55

Explanation: The findFirst() method returns the first element from the stream. The reduce((a, b) -> b) operation keeps the latest element as it processes the stream, so the final value represents the last element.

Note: The Stream API is useful when working with stream-based operations, but for simply accessing the first and last elements, get() is more straightforward.

Comment