Adds an element as the first element of this collection (optional operation). After this operation completes normally, the given element will be a member of this collection, and it will be the first element in encounter order.

package com.logicbig.example.arraylist;
import java.util.ArrayList;
import java.util.Arrays;
public class AddFirstExample {
public static void main(String... args) {
// Add to beginning (Java 21+)
ArrayList<String> list = new ArrayList<>(Arrays.asList("B", "C"));
list.addFirst("A");
System.out.println("After addFirst: " + list);
}
}
Output
After addFirst: [A, B, C]
JDK 25