Java Deque

A deque is a linear collection that supports insertion and deletion of elements from both the ends. The name 'deque' is an abbreviation for double-ended queue.

There are no fixed limits on the deque for the number of elements they may contain. However, this interface supports capacity restricted deques as well as the deques with no fixed size limits. There are various methods which are provided to insert, remove and examine the elements.

These methods typically exist in two forms: the one that throws an exception when the particular operation fails and the other returns a special value which can be either null or false depending upon the operations.

Methods

The java.util.Deque interface provides methods to perform the operations of double-ended queue in Java. Its implementation class is java.util.ArrayDeque.

MethodsDescription
add(E e)This method is used to insert a specified element into the queue represented by the deque
addAll(Collection<? Extends E>c)Adds all the elements in the specified collection at the end of the deque.
addFirst(E e)Inserts the specified element at the front of the deque.
addLast(E e)Inserts the specified element at the end of the deque.
contains(object o)Returns true if the deque contains the specified element.
descendingIterator()Returns an iterator over the elements in reverse sequential order.
element()Retrieves the head of the queue represented by the deque.
getFirst()Retrieves but does not remove the first element of the deque.
getLast()Retrieves but does not remove the last element of the deque.
iterator()Returns an iterator over the element in the deque in a proper sequence.
offer(E e)Inserts the specified element into the deque, returning true upon success and false if no space is available.
offerFirst()Inserts the specified element at the front of the deque unless it violates the capacity restriction.
offerLast()Inserts the specified element at the end of the deque unless it violates the capacity restriction.
peek()Retrieves but does not move the head of the queue represented by the deque or may return null if the deque is empty.
peekFirst()Retrieves but does not move the first element of the deque or may return null if the deque is empty.
peekLast()Retrieves but does not move the last element of the deque or may return null if the deque is empty.
poll()Retrieves and remove the head of the queue represented by the deque or may return null if the deque is empty.
pollFirst()Retrieves and remove the first element of the deque or may return null if the deque is empty.
pollLast()Retrieves and remove the last element of the deque or may return null if the deque is empty.
pop()Pops an element from the stack represented by the deque.
push()Pushes an element onto the stack represented by the deque.
remove()Retrieves and remove the head of the queue represented by the deque.
remove(Object o)Removes the first occurrence of the specified element from the deque.
removeFirst()Retrieves and remove the first element from the deque.
removeFirstOccurrence(Object o)Remove the first occurrence of the element from the deque.
removeLast()Retrieve and remove the last element from the deque.
removeLastOccurrence(Object o)Remove the last occurrence of the element from the deque.
size()Returns the total number of elements in the deque.

Example 1

Test it Now

Output:

Inserting three elements : 
1
2
3
After popping : 
2
3
Removing the element 3 :[2]

Example 2

Test it Now

Output:

The first element is : [Java]
After adding the next element in the front of the deque : [Python, Java]
The final deque is  : [Python, Java, Ruby]
The number of elements are : 3
Deque after removing the last element is given as :  [Python, Java]

Next TopicJava Deque




Latest Courses