Java ArrayDeque getFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, getFirst() returns the head (first element) of an ArrayDeque without removing it. If the deque is empty, it throws NoSuchElementException.

It is the throwing version of peekFirst(), and element() does exactly the same. For a stack built with push, getFirst() reads the top.

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, getFirst is also declared by the SequencedCollection interface, which Deque extends.

Syntax

Call getFirst with no arguments.

Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("north", "east"));
String first = deque.getFirst();    // "north", deque unchanged
CallWhat it does
deque.getFirst()Returns the head; throws if empty.
deque.peekFirst()Returns the head, or null if empty.
deque.element()Same as getFirst().
deque.removeFirst()Returns the head and removes it.

getFirst() is a constant-time read. It never changes the deque, so you can call it as often as you like.

Library declarations
Java
// java.util.ArrayDeque<E>
public E getFirst()

// Declared in Deque<E> (and SequencedCollection<E> since Java 21)

Parameters

ParameterDescription
NonegetFirst() takes no parameters.

Return Value

Return ValueDescription
EThe head of the deque. Never null.

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

OperationHead: throwsHead: special valueTail: throwsTail: special value
InsertaddFirst()offerFirst()addLast()offerLast()
RemoveremoveFirst()pollFirst()removeLast()pollLast()
ExaminegetFirst()peekFirst()getLast()peekLast()

Queue and stack names: add() = addLast(), offer() = offerLast(), push() = addFirst(), pop() = removeFirst(), remove() = removeFirst(), poll() = pollFirst(), element() = getFirst(), peek() = peekFirst().

Java Examples

Example 1 — Read the First Element

Read the head of a deque and confirm nothing was removed.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeGetFirstDemo {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>(Arrays.asList("north", "east", "south"));

        System.out.println("First: " + d.getFirst());
        System.out.println("Size still: " + d.size());
    }
}

How It Works

  1. Three directions. The list order makes north the head.
  2. A pure read. getFirst() returns north.
  3. Size unchanged. All three elements are still in the deque, so size() is 3.
  4. Compare with removeFirst. removeFirst() would also return north, but the size would drop to 2.

Example 2 — Check isEmpty() Before getFirst()

The first round finds nobody waiting; the second round reads the waiting number. A final call on a new deque shows the exception.

Java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.NoSuchElementException;

public class ArrayDequeGetFirstGuard {
    public static void main(String[] args) {
        Deque<Integer> waiting = new ArrayDeque<>();

        for (int round = 1; round <= 2; round++) {
            if (waiting.isEmpty()) {
                System.out.println("round " + round + ": nobody waiting");
                waiting.add(42);
            } else {
                System.out.println("round " + round + ": next is " + waiting.getFirst());
            }
        }

        try {
            new ArrayDeque<Integer>().getFirst();
        } catch (NoSuchElementException e) {
            System.out.println("getFirst() on empty: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Round 1. The deque is empty, so the if branch runs, prints the message, and adds 42.
  2. Round 2. Now isEmpty() is false, so getFirst() is safe and returns 42.
  3. The unguarded call. new ArrayDeque<Integer>().getFirst() runs on an empty deque and throws.
  4. Guard pattern. Testing isEmpty() first is the simplest way to use a throwing method without a try block.

Example 3 — Read the Top of a History Stack

Pages are pushed as you browse; getFirst() reads the current page and getLast() the oldest.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeHistoryTop {
    public static void main(String[] args) {
        Deque<String> history = new ArrayDeque<>();
        history.push("page1");
        history.push("page2");
        history.push("page3");

        System.out.println("Current page: " + history.getFirst());
        history.pop();
        System.out.println("After back: " + history.getFirst());
        System.out.println("Oldest: " + history.getLast());
    }
}

How It Works

  1. Browse three pages. Each push puts the newest page at the head.
  2. Top of the stack. getFirst() reads the head, which is page3.
  3. Go back. pop() removes page3, and getFirst() now returns page2.
  4. The other end. getLast() reads the tail, page1, the first page visited.

Frequently Asked Questions

It returns the first element (head) of the deque without removing it.
getFirst() throws NoSuchElementException. peekFirst() returns null instead.
Yes. element() is the Queue name for the same operation.
getFirst() only reads the head. removeFirst() reads it and removes it.
Since Java 21, List, Deque, and other sequenced collections all declare getFirst() through the SequencedCollection interface.

Next: getLast()

Read the last element; throws if empty.

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

8 people found this page helpful