Java Deque Interface

Category: Java   Tags: Java, Java Collection, Java Deque Interface

The Deque interface extends Queue and declares the behaviour of a double-ended queue. Deque is a generic interface:

                          interface Deque<E>
                                

Here, E specifies the type of objects that the deque will contain.

Methods

void addFirst(E element)
Inserts the specified element at the front of this deque. It throws IllegalStateException if the element cannot be added due to the capacity restrictions.
void addLast(E element)
Inserts the specified element at the end of this deque. It throws IllegalStateException if the element cannot be added due to the capacity restrictions.
boolean offerFirst(E element)
Inserts the specified element at the front of this deque. Returns true if the element was added to this deque otherwise false. It throws IllegalArgumentException if some property of the specified element prevents it from being added to this deque.
boolean offerLast(E element)
Inserts the specified element at the end of this deque. Returns true if the element was added to this deque otherwise false. It throws IllegalArgumentException if some property of the specified element prevents it from being added to this deque.
E element()
Retrieves, but does not remove, the head of this deque. Unlike peek() it throws NoSuchElementException exception if this deque is empty.
E removeFirst()
Retrieves and removes the first element of this deque. It throws NoSuchElementException exception if this deque is empty.
E removeLast()
Retrieves and removes the last element of this deque. It throws NoSuchElementException exception if this deque is empty.
E pollFirst()
Retrieves and removes the first element of this deque. It returns null if this deque is empty.
E pollLast()
Retrieves and removes the last element of this deque. It returns null if this deque is empty.
E getFirst()
Retrieves, but does not remove, the first element of this deque. It throws NoSuchElementException exception if this deque is empty.
E getLast()
Retrieves, but does not remove, the last element of this deque. It throws NoSuchElementException exception if this deque is empty.
E peekFirst()
Retrieves, but does not remove, the first element of this deque. Returns null if this deque is empty.
E peekLast()
Retrieves, but does not remove, the last element of this deque. Returns null if this deque is empty.
boolean removeFirstOccurrence(Object element)
Removes the first occurrence of the specified element from this deque. Returns true if element was removed.
boolean removeLastOccurrence(Object element)
Removes the last occurrence of the specified element from this deque. Returns true if element was removed.
boolean removeLastOccurrence(Object element)
Removes the last occurrence of the specified element from this deque. Returns true if element was removed.
boolean add(E element)
Inserts the specified element into this deque. Returns true if success else false. Throws IllegalStateException if the element cannot be added at this time due to the capacity restrictions.
boolean offer(E element)
Inserts the specified element into this deque. Returns true if success else false if no space is currently available.
E remove()
Retrieves, but does not remove, the head of this deque. It throws NoSuchElementException exception if this deque is empty.
E poll()
Retrieves and removes the first element of this deque. Returns null if this deque is empty.
E element()
Retrieves, but does not remove, the first element of this deque. Throws NoSuchElementException if this deque is empty.
E peek()
Retrieves, but does not remove, the first element of this deque. Returns null if this deque is empty.
void push(E element)
Add an element to head of this deque. Returns true if success. Throws IllegalStateException if no space is currently available.
void pop()
Removes and returns the first element of this deque. Throws NoSuchElementException if this deque is empty.
boolean remove(Object element)
Removes the first occurrence of the specified element from this deque. Returns true if an element was removed.
boolean contains(Object element)
Returns true if this deque contains the specified element.
int size()
Returns the number of elements in this deque.
Iterator<E> iterator()
It returns an iterator over the elements in this deque. The order will be from the top element to the bottom element.

LinkedList is one commonly used class that implements Deque interface.