Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java ArrayDeque pollFirst and pollLast: Usage and Behavior

Understand Java ArrayDeque's pollFirst() and pollLast() methods: their return behavior, differences from removeFirst()/removeLast(), and practical usage examples.

~4 min readJava
#ArrayDeque#Java Collections#Deque#Queue
Diagram showing ArrayDeque with pollFirst and pollLast removing elements from both ends.

ArrayDeque is a resizable-array implementation of the Deque interface. Its pollFirst() and pollLast() methods retrieve and remove elements from the front and back of a deque, and they return null when the deque is empty. This article covers how those methods behave, how they differ from removeFirst() and removeLast(), and how to use them in practical code.

How pollFirst() and pollLast() Work

pollFirst() removes and returns the first element of the deque, while pollLast() removes and returns the last element. Both methods return null if the deque is empty. This behavior is defined in the Deque interface and implemented by ArrayDeque.

java
Deque<String> deque = new ArrayDeque<>(); deque.add("first"); deque.add("second"); deque.add("third"); String head = deque.pollFirst(); // returns "first" String tail = deque.pollLast(); // returns "third"

After these operations, the deque contains only "second". The methods mutate the deque by removing the element they return. If the deque is empty, both methods return null instead of throwing an exception.

Empty Deque: pollFirst()/pollLast() vs removeFirst()/removeLast()

The most important distinction is how the methods handle an empty deque. pollFirst() and pollLast() return null, whereas removeFirst() and removeLast() throw NoSuchElementException. This makes the poll variants safer in scenarios where the deque might be empty and you want to avoid exception handling.

java
Deque<String> emptyDeque = new ArrayDeque<>(); String value = emptyDeque.pollFirst(); // null, no exception String value2 = emptyDeque.removeFirst(); // throws NoSuchElementException

Choosing between these depends on whether an empty deque is a normal condition or an exceptional one. In a task-processing loop where the deque is frequently empty, pollFirst() is the natural choice. If an empty deque indicates a programming error, removeFirst() makes that failure explicit.

Using pollFirst() and pollLast() for Queue and Stack Operations

ArrayDeque can be used as a queue (FIFO) or a stack (LIFO). The poll methods align with these use cases:

  • Queue: addLast() to enqueue, pollFirst() to dequeue.
  • Stack: addFirst() to push, pollFirst() to pop.
java
// Queue usage ArrayDeque<Integer> queue = new ArrayDeque<>(); queue.addLast(10); queue.addLast(20); Integer next = queue.pollFirst(); // 10 // Stack usage ArrayDeque<Integer> stack = new ArrayDeque<>(); stack.addFirst(1); stack.addFirst(2); Integer top = stack.pollFirst(); // 2

For stack operations, pollLast() can also be used if you push with addLast(). The key is to be consistent. The ArrayDeque class is often preferred over Stack for single-threaded code because it is faster and does not carry the legacy synchronization overhead of Stack.

Performance and Memory Characteristics of ArrayDeque

ArrayDeque is backed by a dynamically resizing array. This gives it amortized constant-time performance for addFirst, addLast, pollFirst, and pollLast. The array grows automatically when capacity is exceeded, which involves copying elements. However, the amortized cost remains O(1) per operation.

Memory-wise, ArrayDeque uses a contiguous array, which is more cache-friendly than a linked structure. It also avoids the per-node object overhead of LinkedList. For most use cases, ArrayDeque is faster and uses less memory than LinkedList, especially when the deque grows large.

One limitation is that ArrayDeque does not allow null elements. Attempting to add null throws NullPointerException. This is a deliberate design choice because pollFirst() and pollLast() use null to indicate an empty deque. If you need to store null values, you must use a different Deque implementation like LinkedList.

Thread Safety and Concurrent Access

ArrayDeque is not thread-safe. If multiple threads access the same instance concurrently, external synchronization is required. The methods themselves are not synchronized, so you must coordinate access from multiple threads.

java
Deque<String> safeDeque = Collections.synchronizedDeque(new ArrayDeque<>());

Even if the deque is wrapped in Collections.synchronizedDeque(), compound operations such as checking for emptiness before polling still need additional coordination. For an unbounded concurrent deque, ConcurrentLinkedDeque is a better fit. It is designed for concurrent access and also does not permit null elements.

Practical Example: Task Processing with pollFirst()

A common pattern is a task queue that is drained by a single worker loop. Using pollFirst() allows the loop to retrieve a task without throwing if the queue is empty.

java
ArrayDeque<Runnable> tasks = new ArrayDeque<>(); // ... tasks are added via tasks.addLast(runnable) Runnable task; while ((task = tasks.pollFirst()) != null) { task.run(); }

This loop terminates when the deque becomes empty. The pollFirst() return value is checked for null to stop the loop. This is a clean way to drain a deque without relying on exceptions or separate size checks.

If you need to process the last element first, you would use pollLast() in a similar manner. This is useful for stack-like processing where the most recently added item should be handled first.

When to Choose ArrayDeque Over LinkedList

ArrayDeque is the recommended implementation of Deque for most single-threaded scenarios. It offers better performance due to its array backing and lower memory overhead. LinkedList implements both List and Deque, so it is useful when you need indexed access or the ability to store null elements. However, for pure deque operations, ArrayDeque is almost always the better choice.

CharacteristicArrayDequeLinkedList
Underlying structureResizable arrayDoubly linked list
Null elementsNot allowedAllowed
Indexed accessNot supportedSupported (O(n))
Memory overheadLower (contiguous array)Higher (per-node objects)
Best forQueue/stack, single-threadedList + deque, null elements

The decision comes down to whether you need list-specific features. If your code only uses deque operations, ArrayDeque is the conventional, more efficient default. The pollFirst() and pollLast() methods are an integral part of that efficiency, providing a simple and reliable way to consume elements from either end.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.