Home Java ArrayDeque Methods addFirst() Java ArrayDeque addFirst() Method Definition In Java, addFirst(e) inserts an element at the head (the front) of an ArrayDeque. The elements that were already there now come after it, in the same order. The method returns void.
It is the same operation as push(e) , which is the name you use when the deque acts as a stack. offerFirst(e) also inserts at the head but returns true.
An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety . Since Java 21, addFirst is also declared by the SequencedCollection interface, which Deque extends.
Syntax Call addFirst with the element that should become the new head.
Deque<String> deque = new ArrayDeque<>();
deque.addFirst("b"); // [b]
deque.addFirst("a"); // [a, b] Call What it does deque.addFirst(e)Inserts e at the head. Returns nothing. deque.push(e)Exactly the same; stack-style name. deque.offerFirst(e)Same position, returns true. deque.addFirst(null)Throws NullPointerException.
Because addFirst returns void, code like boolean ok = deque.addFirst(x); does not compile. Use offerFirst if you want a boolean.
Library declarations // java.util.ArrayDeque<E>
public void addFirst(E e)
// Declared in Deque<E> (and SequencedCollection<E> since Java 21) Parameters Parameter Description eRequired. The element to insert at the head. Must not be null.
Return Value Return Value Description voidNothing is returned. The deque has one more element, at the head.
Throw or Return a Special Value? Each end has a method that throws when it cannot work (such as on an empty deque) and one that returns null or false instead. The highlighted cell matches addFirst().
Queue and stack names: add() = addLast(), offer() = offerLast(), push() = addFirst(), pop() = removeFirst(), remove() = removeFirst(), poll() = pollFirst(), element() = getFirst(), peek() = peekFirst().
Java Examples 1. Build from the front 2. Urgent job first 3. Reverse an array Example 1 — Build a Deque from the Front Insert C, then B, then A at the head and see the final order.
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeAddFirstDemo {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.addFirst("C");
deque.addFirst("B");
deque.addFirst("A");
System.out.println(deque);
System.out.println("First: " + deque.getFirst());
System.out.println("Last: " + deque.getLast());
}
} [A, B, C]
First: A
Last: C How It Works The first insert. With an empty deque, C is both the head and the tail.Each new head. B goes in front of C, and then A goes in front of B.Reading the result. The deque prints [A, B, C]: the last element inserted with addFirst is shown first.The ends. getFirst() returns A and getLast() returns C, the very first element that went in.Example 2 — Let an Urgent Job Jump the Queue Normal jobs join at the tail, but an urgent job is placed at the head so it runs next.
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeUrgentJob {
public static void main(String[] args) {
Deque<String> jobs = new ArrayDeque<>();
jobs.addLast("print report");
jobs.addLast("send email");
jobs.addFirst("fix outage");
while (!jobs.isEmpty()) {
System.out.println("Doing: " + jobs.pollFirst());
}
}
} Doing: fix outage
Doing: print report
Doing: send email How It Works Normal order. Two jobs are added at the tail, giving [print report, send email].Priority insert. addFirst("fix outage") places the urgent job ahead of both.Run from the head. The loop takes jobs with pollFirst() until isEmpty() is true.Result. The urgent job runs first, and the other two keep their original order.Example 3 — Reverse an Array with addFirst Adding every value at the head reverses the order without any index math.
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeReverseArray {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4};
Deque<Integer> reversed = new ArrayDeque<>();
for (int n : nums) {
reversed.addFirst(n);
}
System.out.println(reversed);
System.out.println("Size: " + reversed.size());
}
} How It Works Walk the array forward. The enhanced for loop visits 1, 2, 3, and 4 in that order.Each value becomes the head. 1 is inserted first, then 2 goes in front of it, and so on, each value boxed to an Integer.Reversed output. The last value visited, 4, ends up at the head, so the deque prints [4, 3, 2, 1].Nothing lost. All four values are stored, so size() is 4.Frequently Asked Questions What does ArrayDeque addFirst() do? It inserts the element at the head (front) of the deque. It returns nothing.
Is addFirst() the same as push()? Yes. For ArrayDeque, push(e) simply calls addFirst(e). Use push when you think of the deque as a stack.
What is the difference between addFirst() and offerFirst()? Both insert at the head. addFirst returns void; offerFirst returns true. On an ArrayDeque neither can fail for lack of space.
Does addFirst() accept null? No. addFirst(null) throws NullPointerException.
How fast is addFirst()? It runs in amortized constant time: the deque occasionally grows its internal array, but no elements are shifted on each insert.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion