Java ArrayList getFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 21+

Definition

In Java 21 and later, getFirst() returns the first element of an ArrayList, the one at index 0, without removing it.

It reads the same element as get(0), but it fails differently on an empty list: getFirst() throws NoSuchElementException, while get(0) throws IndexOutOfBoundsException. The name matches getFirst() on Deque and LinkedList.

An instance method of java.util.ArrayList, added in Java 21. It was added to List through the new SequencedCollection interface, and ArrayList overrides it.

Java 21+ only.

getFirst() comes from the SequencedCollection interface added in Java 21 (JEP 431). The examples on this page need JDK 21 or newer; on Java 8, 11, or 17 the call fails to compile with "cannot find symbol". On older versions, use get(0) after checking !list.isEmpty().

Syntax

Call getFirst with no arguments (Java 21+).

Java
// Java 21+
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
String first = list.getFirst();   // "a", list unchanged
String same = list.get(0);        // "a" on every Java version
CallWhat it does
list.getFirst()Java 21+: the element at index 0; throws if empty.
list.get(0)Same element on all versions.
list.isEmpty() ? null : list.get(0)A safe read that gives null for an empty list.
list.removeFirst()Java 21+: reads and removes the first element.

getFirst() is a constant-time read and never changes the list.

Library declarations
Java
// java.util.ArrayList<E>  (since Java 21)
public E getFirst()

// Declared in SequencedCollection<E> and List<E> (Java 21)

Parameters

ParameterDescription
NonegetFirst() takes no parameters.

Return Value

Return ValueDescription
EThe first element. Can be null if a null is stored at index 0.

Compare with Similar Methods

Reading the ends of a list:

MethodHow it differs
get(0)Same element on any Java version; IndexOutOfBoundsException when empty.
getLast()Java 21+: reads the other end.
removeFirst()Java 21+: also removes the first element.
ArrayDeque.peekFirst()Returns null on an empty deque instead of throwing.

Java Examples

Example 1 — Who Is First in Line?

Read the first name with getFirst() and with get(0).

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListGetFirstDemo {
    public static void main(String[] args) {
        List<String> line = new ArrayList<>(Arrays.asList("Maya", "Omar", "Lena"));

        System.out.println("first: " + line.getFirst());
        System.out.println("get(0): " + line.get(0));
        System.out.println("list: " + line);
    }
}

How It Works

  1. Front of the line. Maya was listed first, so she is at index 0.
  2. New method, old answer. getFirst() and get(0) return the same element.
  3. No removal. Printing the whole list afterwards still shows all three names.
  4. Java 21 only. This program needs a Java 21 compiler because of the getFirst() call; the rest would compile on Java 8.

Example 2 — getFirst() vs get(0) on an Empty List

Both calls fail on an empty list, with different exceptions. A guarded read avoids both.

Java
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

public class ArrayListGetFirstEmpty {
    public static void main(String[] args) {
        List<String> empty = new ArrayList<>();

        try {
            empty.getFirst();
        } catch (NoSuchElementException e) {
            System.out.println("getFirst: " + e.getClass().getSimpleName());
        }
        try {
            empty.get(0);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("get(0): IndexOutOfBoundsException");
        }
        String safe = empty.isEmpty() ? "(none)" : empty.getFirst();
        System.out.println("safe read: " + safe);
    }
}

How It Works

  1. No first element. On an empty list, getFirst() throws NoSuchElementException.
  2. Different exception, same problem. get(0) fails too, but with IndexOutOfBoundsException, because index 0 is not below size(), which is 0.
  3. Guarded read. The conditional expression checks isEmpty() first and picks "(none)" without ever calling getFirst().
  4. Pick one style. Catching the exception works, but the isEmpty() check is simpler and cheaper when an empty list is a normal case.

Example 3 — Read the Smallest Value After Sorting

Sort lap times, then read the fastest and slowest from the two ends.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListGetFirstSorted {
    public static void main(String[] args) {
        List<Integer> times = new ArrayList<>(Arrays.asList(42, 17, 58, 23));
        times.sort(null);

        System.out.println("sorted: " + times);
        System.out.println("fastest: " + times.getFirst() + "s");
        System.out.println("slowest: " + times.getLast() + "s");
    }
}

How It Works

  1. Lap times. Four Integer values start in no particular order.
  2. Natural order. sort(null) arranges the numbers from smallest to largest.
  3. Smallest first. After sorting, getFirst() returns the minimum, 17 seconds.
  4. Largest last. getLast() reads the maximum, 58, from the other end of the sorted list.

Frequently Asked Questions

Java 21. It comes from the SequencedCollection interface, which List extends since that release.
It returns the same element. The difference is the exception on an empty list: NoSuchElementException for getFirst(), IndexOutOfBoundsException for get(0).
NoSuchElementException. Check isEmpty() before calling it if the list may be empty.
No. It only reads it. removeFirst() reads and removes.
Use list.get(0) after checking that the list is not empty.

Next: getLast()

Read the last element (Java 21+); throws if empty.

getLast() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful