Java ArrayDeque pollFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, pollFirst() removes the head (first element) of an ArrayDeque and returns it, or returns null if the deque is empty.

It is the "special value" version of removeFirst(), and the Deque name for poll(). Its mirror at the tail is pollLast().

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.

Syntax

Call pollFirst with no arguments.

Java
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(1, 2, 3));
Integer head = deque.pollFirst();    // 1, deque = [2, 3]
CallWhat it does
deque.pollFirst()Removes and returns the head, or null.
deque.poll()Same operation, Queue name.
deque.removeFirst()Same, but throws when empty.
deque.pollLast()Removes from the tail instead.

Store the result in a wrapper type such as Integer or String and test it for null before using it. This lets you write loops without a separate isEmpty() check.

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

// Declared in Deque<E>

Parameters

ParameterDescription
NonepollFirst() takes no parameters.

Return Value

Return ValueDescription
EThe former head element.
nullIf the deque was empty.

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

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 — Take the First Element

Remove the head, then clear the deque and see what pollFirst() returns.

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

public class ArrayDequePollFirstDemo {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(Arrays.asList(1, 2, 3));

        Integer head = d.pollFirst();
        System.out.println("got " + head + ", left " + d);

        d.clear();
        System.out.println("empty deque gives " + d.pollFirst());
    }
}

How It Works

  1. Head removed. pollFirst() returns 1 and the deque becomes [2, 3].
  2. Wrapper type. head is an Integer, so it could also hold null safely.
  3. Empty it. clear() removes the remaining two elements.
  4. No exception. On the empty deque, pollFirst() returns null, which string concatenation prints as null.

Example 2 — Process Orders in Batches of Two

Each batch polls up to two orders. When the deque runs dry, pollFirst() returns null and nothing is added.

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

public class ArrayDequeBatches {
    public static void main(String[] args) {
        Deque<String> orders = new ArrayDeque<>(Arrays.asList("o1", "o2", "o3", "o4"));

        for (int batch = 1; batch <= 3; batch++) {
            List<String> taken = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                String o = orders.pollFirst();
                if (o != null) {
                    taken.add(o);
                }
            }
            System.out.println("batch " + batch + ": " + taken);
        }
    }
}

How It Works

  1. Four orders waiting. They will be taken from the head in the order o1 to o4.
  2. Up to two per batch. The inner loop polls twice and keeps each non-null result.
  3. Batches 1 and 2. They take o1 and o2, then o3 and o4.
  4. Batch 3 is empty. Both polls return null, so the list stays empty and prints [], with no exception along the way.

Example 3 — Take From the Front and Back in Turn

Alternate between pollFirst() and pollLast() until the deque is empty.

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

public class ArrayDequeZigZag {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4, 5));
        StringBuilder sb = new StringBuilder();
        boolean front = true;

        while (!d.isEmpty()) {
            sb.append(front ? d.pollFirst() : d.pollLast()).append(' ');
            front = !front;
        }
        System.out.println(sb.toString().trim());
    }
}

How It Works

  1. Start at the front. front is true, so the first value taken is 1 from the head.
  2. Switch ends. The flag flips each pass, so the next value is 5 from the tail.
  3. Close in from both sides. The pattern continues with 2 from the head and 4 from the tail, and finally 3, the middle value.
  4. Tidy output. Each value is followed by a space, so trim() removes the trailing one before printing.

Frequently Asked Questions

It removes and returns the first element (head), or returns null if the deque is empty.
Both remove the head. pollFirst() returns null on an empty deque; removeFirst() throws NoSuchElementException.
Yes. poll() is the Queue name and pollFirst() is the Deque name for the same operation.
Only if you are sure the deque is not empty. Unboxing a null result throws NullPointerException.
Use pollLast() for the tail, or removeLast() if you want an exception on an empty deque.

Next: pollLast()

Remove and return the tail, or null if empty.

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

10 people found this page helpful