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)
Call
What 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
Parameter
Description
None
removeFirst() takes no parameters.
Return Value
Return Value
Description
E
The element that was removed from index 0 (may be null).
One element.removeFirst() returns 5, and because the string parts are evaluated left to right, size() afterwards reports 0.
Nothing left. A second removeFirst() throws NoSuchElementException.
Index style.remove(0) on the same empty list throws IndexOutOfBoundsException instead.
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);
}
}
}
Output
played song1 -> [song2, song3, song1]
played song2 -> [song3, song1, song2]
How It Works
Play the front song.removeFirst() takes the first song off the list and returns it.
Back of the line.addLast(played) puts that same song at the end, so nothing is lost.
Rotation. After two passes the playlist has turned twice, and song3 is now first.
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.