Deque interface in Java

Java provides us the Deque interface is the subtype of Queue interface in Collection framework.

Deque represents a queue where you can insert and remove elements from both ends of the queue. Thus, “Deque” is short for “Double Ended Queue”.


Deque is a linear collection of objects which supports insertion and removal of elements from both the ends. The Deque interface defines the methods needed to insert, retrieve and remove the elements from both the ends.The Deque interface is introduced in Java SE 6.


Methods in Deque Interface

  1. add () – To add elements at the tail.
  2. addFirst() – To add the element at the Head.
  3. addLast() – To add elements at the tail.
  4. push() – To add the element at the Head.
  5. pop() – To remove the element from the Head.
  6. remove() – To remove elements from the tail.
  7. removeFirst() – To remove the element from the Head.
  8. removeLast() – To remove elements from the tail.


Implementation of Deque

Deque is an interface you need to instantiate a concrete implementation of the interface in order to use it. You can choose between the following Deque implementations in the Java Collections API:

  • java.util.ArrayDeque

  • java.util.LinkedList

LinkedList is a pretty standard deque/queue implementation.

ArrayDeque stores its elements internally in an array. If the number of elements exceeds the space in the array, a new array is allocated, and all elements moved over. In other words, the ArrayDeque grows as needed, even if it stores its elements in an array.


Here are a few examples of how to create a Deque instance:

Deque dequeA = new LinkedList();

Deque dequeB = new ArrayDeque();

Let’s see the example of Deque –


Deque contains : [Anshul, chauhan, Mirilla, Knightly]
After add elemnets Queue is — [Selenium, Anshul, chauhan, Mirilla, Knightly, Bix]
After remove element Queue is — [Anshul, chauhan, Mirilla, Knightly]


We have implemented the Deque in above example code. Let’s understand the above code.

First, we have implemented the Deque then we added elements to the queue using add() method . Then we print the Deque elements.

After that, we added the elements at the Head and Tail of the queue and print the queue elements.

After that, we removed the elements from both ends and print the queue elements.


We have seen, how can we perform insertion and deletion from the both end of the queue.


Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply