Deque Interface - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Saturday, November 26, 2016

Deque Interface

The Deque interface extends Queue interface and declare the behavior of 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:

public interface Deque<E> extends Queue<E>

Here, E specifies the type of objects that the list will hold.

In addition to the methods defined by Queue, Deque defines some of its own method.

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. The object is not removed. It returns null if the deque is empty.
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 removeFirstOccurrence(Object obj) Removes the first occurrence of obj from the deque. Returns true 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 removeLastOccurrence(Object obj) Removes the last occurrence of obj from the deque. Returns true if successful and false if the deque did not contain obj.

Most Commonly thrown Exceptions in Queue

Exception Description
ClassCastException occurs when an attempt is made to add an incompatible object in a deque
NullPointerException will throw when you try to store a null object and null element is not allowed in the deque.
IllegalArgumentException will throw when an invalid argument is used.
IllegalStateException thrown if an attempt is made to add an element to a fixed-length deque that is full.
NoSuchElementException thrown if an attempt is made to remove an element from an empty deque.

No comments :

Post a Comment