Java ArrayList addFirst() Method

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

Definition

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)
CallWhat 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

ParameterDescription
element (E)The element to insert at index 0. May be null.

Return Value

Return ValueDescription
voidNothing is returned.

Compare with Similar Methods

Ways to put an element at the front, and their neighbors:

MethodHow it differs
add(0, e)Same effect; works before Java 21.
addLast(e)Java 21+: puts the element at the other end.
removeFirst()Java 21+: takes the first element back out.
ArrayDeque.addFirst(e)Constant time at the front, but no index access.

Java Examples

Example 1 — Put the Newest Message First

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));
    }
}

How It Works

  1. Empty start. The first addFirst on an empty list simply makes "hello" the only element.
  2. Each call goes to index 0. "meeting at 3" and then "lunch?" are put in front, pushing older messages toward the end.
  3. Reverse arrival order. The list reads newest to oldest: the message added last prints first.
  4. 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));
    }
}

How It Works

  1. Two identical lists. a and b both start as [2, 3].
  2. Two spellings. a uses the Java 21 method and b the classic indexed add; each puts 1 in front.
  3. Same content. List.equals compares the elements pair by pair in order, so it prints true.
  4. 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.

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

public class ArrayListAddFirstFixed {
    public static void main(String[] args) {
        List<String> fixed = Arrays.asList("x", "y");
        try {
            fixed.addFirst("w");
        } catch (UnsupportedOperationException e) {
            System.out.println("fixed list: " + e.getClass().getSimpleName());
        }

        List<String> growable = new ArrayList<>(fixed);
        growable.addFirst("w");
        growable.addFirst(null);
        System.out.println(growable + " size=" + growable.size());
    }
}

How It Works

  1. Cannot grow. The list from Arrays.asList has a fixed size, so addFirst throws UnsupportedOperationException.
  2. A growable copy. Copying into new ArrayList<>(fixed) gives a list that accepts inserts at any position.
  3. null is allowed. Unlike ArrayDeque, an ArrayList can store null, so addFirst(null) works and null prints first.
  4. Order check. Two front inserts on [x, y] give [null, w, x, y], four elements in total.

Frequently Asked Questions

Java 21, through the SequencedCollection interface (JEP 431). On older versions use list.add(0, element).
Yes, it gives the same result: the element is inserted at index 0 and the others shift right.
No. Every existing element has to move one slot, so the cost grows with the list size. ArrayDeque adds at the front in constant time.
No, its return type is void. This differs from add(e), which returns true.
You are compiling with a JDK older than 21, or targeting an older release with --release. Upgrade to JDK 21 or use add(0, e).

Next: addLast()

Append an element at the end (Java 21+).

addLast() 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.

9 people found this page helpful