In Java, ArrayDeque has two remove methods. remove() with no argument takes the head off the deque and returns it, just like removeFirst(). If the deque is empty, it throws NoSuchElementException.
remove(Object o) searches from the head and removes the first element equal too (compared with equals). It returns true if something was removed and false otherwise. It is the same as removeFirstOccurrence(o).
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 it with no argument to take the head, or with a value to remove that value.
Removes and returns the head. Throws on an empty deque.
deque.remove(o)
Removes the first element equal to o; returns true or false.
deque.poll()
Like remove(), but returns null when empty.
deque.remove(null)
Returns false; an ArrayDeque never contains null.
remove() runs in constant time. remove(Object) has to search, so it takes time proportional to the size of the deque. A Deque has no remove(int index) method, so remove(5) on a Deque<Integer> removes the value 5.
Library declarations
Java
// java.util.ArrayDeque<E>
public E remove() // same as removeFirst()
public boolean remove(Object o) // same as removeFirstOccurrence(o)
// Declared in Queue<E>, Collection<E>, and Deque<E>
Parameters
Parameter
Description
(none)
remove() takes no argument and works on the head.
o
remove(Object o) only. The value to remove. Matching uses o.equals(element).
Return Value
Return Value
Description
E
remove(): the element that was at the head.
boolean
remove(o): true if an element was removed, false if no match was found.
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 remove().