Java ArrayDeque poll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, poll() removes the head of an ArrayDeque and returns it. If the deque is empty, it returns null instead of throwing an exception.

It comes from the Queue interface and does the same thing as pollFirst(). Together with offer() and peek() it forms the usual first-in, first-out queue.

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 poll with no arguments and check the result for null.

Java
Queue<String> queue = new ArrayDeque<>(Arrays.asList("a", "b"));
String head = queue.poll();      // "a"
String next = queue.poll();      // "b"
String none = queue.poll();      // null (queue is empty)
CallWhat it does
queue.poll()Removes and returns the head, or null if empty.
queue.remove()Same, but throws NoSuchElementException if empty.
deque.pollFirst()The Deque name for the same operation.
queue.peek()Returns the head without removing it.

poll() always works at the head, no matter how the elements got there. If they were added with push, the head is the most recently pushed element.

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

// Declared in Queue<E> and Deque<E>

Parameters

ParameterDescription
Nonepoll() 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 poll().

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 — Poll Past the End

Poll a two-element queue three times.

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

public class ArrayDequePollDemo {
    public static void main(String[] args) {
        Queue<String> q = new ArrayDeque<>(Arrays.asList("a", "b"));

        System.out.println(q.poll());
        System.out.println(q.poll());
        System.out.println(q.poll());
        System.out.println(q);
    }
}

How It Works

  1. First poll. Returns a, the head, and removes it.
  2. Second poll. Returns b, which had become the head.
  3. Third poll. The queue is empty, so poll() returns null and println prints the word null.
  4. Empty brackets. An empty deque prints as [].

Example 2 — Visit a Tree Level by Level

Breadth-first search uses a queue: poll() the next node, then offer() its children.

Java
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

public class ArrayDequeBfs {
    public static void main(String[] args) {
        String[] names = {"root", "left", "right", "left.kid", "right.kid"};
        int[][] children = {{1, 2}, {3}, {4}, {}, {}};

        Queue<Integer> queue = new ArrayDeque<>();
        List<String> visited = new ArrayList<>();
        queue.offer(0);

        Integer node;
        while ((node = queue.poll()) != null) {
            visited.add(names[node]);
            for (int child : children[node]) {
                queue.offer(child);
            }
        }
        System.out.println(visited);
    }
}

How It Works

  1. A tiny tree. Node 0 (root) has children 1 and 2; node 1 has child 3; node 2 has child 4.
  2. Start at the root. Only node 0 is in the queue at first. Polling it records root and offers 1 and 2.
  3. One level at a time. Nodes 1 and 2 are polled before 3 and 4, because they were offered earlier. That is what makes the search breadth-first.
  4. Loop ends on null. When every node has been visited, poll() returns null and the while condition becomes false.

Example 3 — poll Always Takes the Head

Elements pushed as a stack and then polled come out newest first, because push and poll both use the head.

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

public class ArrayDequePollHead {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>();
        d.push("first pushed");
        d.push("second pushed");

        System.out.println(d.poll());

        d.offer("offered");
        System.out.println(d);
        System.out.println(d.poll());
    }
}

How It Works

  1. Two pushes. The second push lands in front of the first, so the head is "second pushed".
  2. poll reads the head. It returns "second pushed"; it does not care which method added the element.
  3. offer goes to the other end. "offered" is added at the tail, giving [first pushed, offered].
  4. Next poll. The head is now "first pushed", so that is what the last line prints.

Frequently Asked Questions

It removes and returns the head of the deque, or returns null if the deque is empty.
Both remove the head. poll() returns null on an empty deque; remove() throws NoSuchElementException.
Yes. For ArrayDeque, poll() does exactly what pollFirst() does.
From the front (head). Use pollLast() to remove from the back.
ArrayDeque never stores null, so null from poll() can only mean the deque was empty.

Next: pollFirst()

Remove and return the head, or null if empty.

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