In Java 21 and later, addFirst(e) inserts an element at the front of an ArrayList, at index 0. Every element already in the list moves one position to the right, so the old first element ends up at index 1.
It does exactly what add(0, e) has always done; the new name simply matches addFirst on Deque and LinkedList, so all sequenced collections share one vocabulary. It returns void.
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.
addFirst() 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 add(0, e).
Syntax
Call addFirst with the element to insert (Java 21+).
Java
// Java 21+
List<String> list = new ArrayList<>(Arrays.asList("b", "c"));
list.addFirst("a"); // [a, b, c]
list.add(0, "z"); // [z, a, b, c] (works on every Java version)
Call
What it does
list.addFirst(e)
Java 21+: inserts e at index 0.
list.add(0, e)
Same result on any Java version.
list.addLast(e)
Java 21+: appends at the other end.
deque.addFirst(e)
On an ArrayDeque, a cheap constant-time operation.
Inserting at the front of an ArrayList shifts all existing elements, so it takes time proportional to the list size. If you insert at the front often, an ArrayDeque is a better fit.
Library declarations
Java
// java.util.ArrayList<E> (since Java 21)
public void addFirst(E element)
// Declared in SequencedCollection<E> and List<E> (Java 21)
Parameters
Parameter
Description
element (E)
The element to insert at index 0. May be null.
Return Value
Return Value
Description
void
Nothing is returned.
Compare with Similar Methods
Ways to put an element at the front, and their neighbors:
Each new message goes in front, so the list reads newest to oldest.
Java
import java.util.ArrayList;
import java.util.List;
public class ArrayListAddFirstDemo {
public static void main(String[] args) {
List<String> inbox = new ArrayList<>();
inbox.addFirst("hello");
inbox.addFirst("meeting at 3");
inbox.addFirst("lunch?");
System.out.println(inbox);
System.out.println("newest: " + inbox.get(0));
}
}
Output
[lunch?, meeting at 3, hello]
newest: lunch?
How It Works
Empty start. The first addFirst on an empty list simply makes "hello" the only element.
Each call goes to index 0. "meeting at 3" and then "lunch?" are put in front, pushing older messages toward the end.
Reverse arrival order. The list reads newest to oldest: the message added last prints first.
Index 0 is the front.get(0) confirms that "lunch?" is now the first element.
Example 2 — addFirst(e) vs add(0, e)
Apply both spellings to identical lists and compare the results.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListAddFirstVsIndex {
public static void main(String[] args) {
List<Integer> a = new ArrayList<>(Arrays.asList(2, 3));
List<Integer> b = new ArrayList<>(Arrays.asList(2, 3));
a.addFirst(1);
b.add(0, 1);
System.out.println("addFirst: " + a);
System.out.println("add(0,e): " + b);
System.out.println("equal lists? " + a.equals(b));
System.out.println("old first moved to index " + a.indexOf(2));
}
}
Output
addFirst: [1, 2, 3]
add(0,e): [1, 2, 3]
equal lists? true
old first moved to index 1
How It Works
Two identical lists.a and b both start as [2, 3].
Two spellings.a uses the Java 21 method and b the classic indexed add; each puts 1 in front.
Same content.List.equals compares the elements pair by pair in order, so it prints true.
Shifted element. The old first value, 2, is now found at index 1.
Example 3 — addFirst on a Fixed-Size List
addFirst fails on Arrays.asList but works on an ArrayList copy, even with null.