Deque interface in Java with Example
The java.util.Deque interface is a subtype of the java.util.Queue interface. The Deque is related to the double-ended queue that supports the addition or removal of elements from either end of the data structure, it can be used as a queue (first-in-first-out/FIFO) or as a stack (last-in-first-out/LIFO). These are faster than Stack and LinkedList.
This is the hierarchy of the Deque interface in Java:
Few important features of Deque are:
- It provides the support of resizable array and helps in restriction-free capacity, so to grow the array according to the usage.
- Array deques prohibit the use of Null elements and do not accept any such elements.
- Any concurrent access by multiple threads is not supported.
- In the absence of external synchronization, Deque is not thread-safe.
Methods of Deque:
- add(element): Adds an element to the tail.
- and first(element): Adds an element to the head.
- addLast(element): Adds an element to the tail.
- offer(element): Adds an element to the tail and returns a boolean to explain if the insertion was successful.
- offer first(element): Adds an element to the head and returns a boolean to explain if the insertion was successful.
- offerLast(element): Adds an element to the tail and returns a boolean to explain if the insertion was successful.
- iterator(): Return an iterator for this deque.
- descendingIterator(): Returns an iterator that has the reverse order for this deque.
- push(element): Adds an element to the head.
- pop(element): Removes an element from the head and returns it.
- remove first(): Removes the element at the head.
- remove last(): Removes the element at the tail.
- poll(): Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
- pollFirst(): Retrieves and removes the first element of this deque, or returns null if this deque is empty.
- a poll last(): Retrieves and removes the last element of this deque, or returns null if this deque is empty.
- peek(): Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
- seek first(): Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
- peekLast(): Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
Summarizing the methods, we get:
Below program illustrates few operations of Deque interface:
filter_none
edit
play_arrow
brightness_4
|
|
Output:
[Element 6 (Head), Element 4 (Head), Element 2 (Head), Element 1 (Tail),
Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)]
Standard Iterator
Element 6 (Head)
Element 4 (Head)
Element 2 (Head)
Element 1 (Tail)
Element 3 (Tail)
Element 5 (Tail)
Element 7 (Tail)
Reverse Iterator
Element 7 (Tail)
Element 5 (Tail)
Element 3 (Tail)
Element 1 (Tail)
Element 2 (Head)
Element 4 (Head)
Element 6 (Head)
Peek Element 6 (Head)
After peek: [Element 6 (Head), Element 4 (Head), Element 2 (Head), Element 1 (Tail),
Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)]
Pop Element 6 (Head)
After pop: [Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail),
Element 5 (Tail), Element 7 (Tail)]
Contains element 3: true
Deque after removing first and last: [Element 2 (Head), Element 1 (Tail),
Element 3 (Tail), Element 5 (Tail)]
Deque in Collection Hierarchy




post a comment