The Deque interface was added by Java SE 6. It extends Queue and declares the behavior of a double-ended queue. Double-ended queues can function as standard, first-in, first-out queues or as last-in, first-out stacks. Deque is a generic interface that has this declaration:
interface Deque<E>
Here, E specifies the type of objects that the deque will hold
The Methods Defined by Deque
Method | Description |
void addFirst(E obj) | Adds obj to the head of the deque. Throws an IllegalStateException |
if a capacity-restricted deque is out of space. | |
void addLast(E obj) | Adds obj to the tail of the deque. Throws an IllegalStateException |
if a capacity-restricted deque is out of space. | |
Iterator<E> descendingIterator( ) | Returns an iterator that moves from the tail to the head of the |
deque. In other words, it returns a reverse iterator. | |
E getFirst( ) | Returns the first element in the deque. The object is not removed |
from the deque. It throws NoSuchElementException if the deque | |
is empty. | |
E getLast( ) | Returns the last element in the deque. The object is not removed |
from the deque. It throws NoSuchElementException if the deque is | |
empty. | |
boolean offerFirst(E obj) | Attempts to add obj to the head of the deque. Returns true if |
obj was added and false otherwise. Therefore, this method | |
returns false when an attempt is made to add obj to a full, | |
capacity-restricted deque. | |
boolean offerLast(E obj) | Attempts to add obj to the tail of the deque. Returns true if obj |
was added and false otherwise. | |
E peekFirst( ) | Returns the element at the head of the deque. It returns null if |
the deque is empty. The object is not removed. | |
E peekLast( ) | Returns the element at the tail of the deque. It returns null if the |
deque is empty. The object is not removed. | |
E pollFirst( ) | Returns the element at the head of the deque, removing the |
element in the process. It returns null if the deque is empty. | |
E pollLast( ) | Returns the element at the tail of the deque, removing the |
element in the process. It returns null if the deque is empty. | |
E pop( ) | Returns the element at the head of the deque, removing it in the |
process. It throws NoSuchElementException if the deque is empty. | |
void push(E obj ) | Adds obj to the head of the deque. Throws an IllegalStateException |
if a capacity-restricted deque is out of space. | |
E removeFirst( ) | Returns the element at the head of the deque, removing the |
element in the process. It throws NoSuchElementException if | |
the deque is empty. | |
boolean | Removes the first occurrence of obj from the deque. Returns true |
removeFirstOccurrence(Object obj) | if successful and false if the deque did not contain obj. |
E removeLast( ) | Returns the element at the tail of the deque, removing the element |
in the process. It throws NoSuchElementException if the deque | |
is empty. | |
boolean | Removes the last occurrence of obj from the deque. Returns true |
removeLastOccurrence(Object obj) | if successful and false if the deque did not contain obj. |