In Java, removeFirst() removes the head (first element) of an ArrayDeque and returns it. The next element becomes the new head.
If the deque is empty, it throws NoSuchElementException. Its "special value" twin, pollFirst(), returns null instead. pop() and remove() do exactly the same as removeFirst().
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, removeFirst is also declared by the SequencedCollection interface, which Deque extends.
Syntax
Call removeFirst with no arguments and use the returned element.
Java
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(10, 20, 30));
int first = deque.removeFirst(); // 10, deque = [20, 30]
Call
What it does
deque.removeFirst()
Removes and returns the head; throws if empty.
deque.pollFirst()
Same, but returns null if empty.
deque.pop()
Same as removeFirst(); stack-style name.
deque.remove()
Same as removeFirst(); queue-style name.
Use removeFirst when an empty deque would be a bug in your program: the exception points straight at the problem. When an empty deque is a normal situation, check isEmpty() first or use pollFirst().
Library declarations
Java
// java.util.ArrayDeque<E>
public E removeFirst()
// Declared in Deque<E> (and SequencedCollection<E> since Java 21)
Parameters
Parameter
Description
None
removeFirst() takes no parameters.
Return Value
Return Value
Description
E
The element that was at the head. Never null, because an ArrayDeque cannot hold 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 returnsnull or false instead. The highlighted cell matches removeFirst().