Java ArrayList removeFirst() Method

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

Definition

In Java 21 and later, removeFirst() removes the first element of an ArrayList and returns it. The remaining elements each move one position to the left, so the old index 1 becomes index 0.

It does the same job as remove(0), except on an empty list: removeFirst() throws NoSuchElementException, while remove(0) throws IndexOutOfBoundsException.

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.

removeFirst() 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 remove(0) after checking !list.isEmpty().

Syntax

Call removeFirst with no arguments (Java 21+).

Java
// Java 21+
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
String first = list.removeFirst();   // "a", list = [b, c]
String next = list.remove(0);        // "b", list = [c]  (any Java version)
CallWhat it does
list.removeFirst()Java 21+: removes and returns index 0.
list.remove(0)The same on every Java version.
list.getFirst()Reads index 0 without removing it.
deque.pollFirst()On an ArrayDeque: removes the head, or returns null when empty.

Every remaining element shifts left after the removal, so removeFirst takes time proportional to the size. Using an ArrayList as a queue with repeated removeFirst calls gets slow on big lists; ArrayDeque removes from the front in constant time.

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

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

Parameters

ParameterDescription
NoneremoveFirst() takes no parameters.

Return Value

Return ValueDescription
EThe element that was removed from index 0 (may be null).

Compare with Similar Methods

Removing from the front, and related reads:

MethodHow it differs
remove(0)Same effect on any Java version; IndexOutOfBoundsException when empty.
removeLast()Java 21+: removes from the other end without shifting anything.
getFirst()Java 21+: reads the first element and leaves it in place.
ArrayDeque.pollFirst()Constant time, and returns null instead of throwing.

Java Examples

Example 1 — Serve Customers in Order

Take customers off the front of the list until nobody is waiting.

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

public class ArrayListRemoveFirstDemo {
    public static void main(String[] args) {
        List<String> queue = new ArrayList<>(Arrays.asList("Ivy", "Jon", "Kim"));

        while (!queue.isEmpty()) {
            String next = queue.removeFirst();
            System.out.println("serving " + next + ", waiting " + queue);
        }
    }
}

How It Works

  1. Loop until empty. The isEmpty() check makes sure removeFirst() is never called on an empty list.
  2. Take from the front. Each call removes and returns the name currently at index 0.
  3. Shift left. After Ivy is removed, Jon moves to index 0 and becomes the next one served.
  4. Drained. The last pass removes Kim and prints the empty list [].

Example 2 — removeFirst() vs remove(0) on an Empty List

Remove the only element, then compare the two methods on the now-empty list.

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

public class ArrayListRemoveFirstEmpty {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(5);
        System.out.println("removed " + nums.removeFirst() + ", size " + nums.size());

        try {
            nums.removeFirst();
        } catch (NoSuchElementException e) {
            System.out.println("removeFirst: " + e.getClass().getSimpleName());
        }
        try {
            nums.remove(0);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("remove(0): IndexOutOfBoundsException");
        }
    }
}

How It Works

  1. One element. removeFirst() returns 5, and because the string parts are evaluated left to right, size() afterwards reports 0.
  2. Nothing left. A second removeFirst() throws NoSuchElementException.
  3. Index style. remove(0) on the same empty list throws IndexOutOfBoundsException instead.
  4. Catch the right type. Code that switches from remove(0) to removeFirst() must also change the exception type it catches.

Example 3 — Rotate a Playlist

Play the first song, then move it to the back of the list.

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

public class ArrayListRemoveFirstRotate {
    public static void main(String[] args) {
        List<String> playlist = new ArrayList<>(Arrays.asList("song1", "song2", "song3"));

        for (int i = 0; i < 2; i++) {
            String played = playlist.removeFirst();
            playlist.addLast(played);
            System.out.println("played " + played + " -> " + playlist);
        }
    }
}

How It Works

  1. Play the front song. removeFirst() takes the first song off the list and returns it.
  2. Back of the line. addLast(played) puts that same song at the end, so nothing is lost.
  3. Rotation. After two passes the playlist has turned twice, and song3 is now first.
  4. Size stays 3. One removal and one append per pass keep the playlist length constant.

Frequently Asked Questions

Java 21, through the SequencedCollection interface. On older versions use remove(0).
It removes and returns the same element. On an empty list removeFirst() throws NoSuchElementException, while remove(0) throws IndexOutOfBoundsException.
NoSuchElementException. Check isEmpty() first if the list might be empty.
Only for small lists. Every removeFirst() shifts the remaining elements. ArrayDeque is designed for removing from the front.
Call list.remove(0) after checking that the list is not empty.

Next: removeIf()

Remove every element that matches a condition (Java 8+).

removeIf() 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