The java.util.Deque interface is a subtype of the java.util.Queue interface. In this type of queue, you can add and remove elements from both the ends and hence it is abbreviated as “Double Ended Queue” and pronounced as “deck” in short.
Lets see the basic operations using Dequeue
Implementation
Since Deque is an interface,we need to instantiate a concrete implementation of the interface in order to use it. You can choose between the following Deque implementations of Java Collections.
- java.util.LinkedList
- java.util.ArrayDeque
Examples of creating Queue Instance
Queue queue1 = new LinkedList(); Queue queue2 = new ArrayDeque();
The order in which the elements are stored internally depends upon the type of implementation that we choose.
Adding elements to the queue
To add element the deque we can use any of the below methods.
add() method inserts element to the head of the deque. This throws IllegalStateException” if no space is currently available. When using a capacity-restricted deque
addFirst() method also inserts element to the head of the deque. This throws IllegalStateException” if no space is currently available. When using a capacity-restricted deque
addLast() method to insert element to the tail of the deque. These method throws IllegalStateException” if no space is currently available. When using a capacity-restricted deque. These method returns a boolean, if the insertion is successful it will return “true” else it will return “false”.
Deque deque = new LinkedList(); deque.add("Java"); deque.addFirst("jQuery"); deque.addLast("HTML5");
Other way of adding element to the queue is through offer() method. All the method will throw IllegalStateException” if no space is currently available. When using a capacity-restricted deque
offer() method inserts element to the head of the deque.
offerFirst() method also inserts element to the head of the deque.
offerLast() method to insert element to the tail of the deque.
deque.offer("AngualarJS"); deque.offerFirst("NodeJS"); deque.offerLast("Javascript");
Accessing elements of the queue
Elements of the queue can be accessed in two ways.
Using getFirst() method / getLast() method, This method returns the head/ tail element of the deque without removing from the queue. It throws “NoSuchElementException” when the queue is empty.
deque.getFirst() deque.getLast()
Using peekFirst()/peekLast() method, This method also returns the head/tail element of the deque without removing from the queue. It returns “null” when the queue is empty.
deque.peekFirst() deque.peekLast()
Removing elements from the queue
Using removeFirst()/pop() method, This method removes and returns the first element of the deque, will throw “NoSuchElementException” when the queue is empty.
deque.removeFirst() deque.pop()
Using removeLast() method, This method removes and returns the last element of the deque, will throw “NoSuchElementException” when the queue is empty.
deque.removeLast()
Code Snippet
import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; import java.util.NoSuchElementException; import java.util.Queue; public class DequeExample { public static void main(String[] args) { //Creating a queue object through LinkedList Deque deque = new LinkedList(); //Adding elements to the deque deque.add("Java"); //addFirst() adds the element to the head of the deque deque.addFirst("jQuery"); //addFirst() adds the element to the tail of the deque deque.addLast("HTML5"); //offer() adds the elements to the deque and returns a boolean deque.offer("AngualarJS"); //offerFirst() adds the element to the head of the deque and returns a boolean deque.offerFirst("NodeJS"); //offerFirst() adds the element to the tail of the deque and returns a boolean deque.offerLast("Javascript"); System.out.println("Elements of the deque"+deque); //getFirst() Will retrive the head of the deque System.out.println("First element of the deque before removal:"+deque.getFirst()); //The removeFirst() &pop() method will remove the first element of the queue deque.removeFirst(); deque.pop(); //peekFirst() Will retrive the head of the deque System.out.println("First element of the deque after removal:"+deque.peekFirst()); //getLast() Will retrive the tail of the deque System.out.println("Last element of the deque before removal:"+deque.getLast()); //The removeLast() method will remove the tail element of the queue deque.removeLast(); //peekLast() Will retrive the tail of the deque System.out.println("Last element of the deque after removal:"+deque.peekLast()); // Iterate through the queue elements. System.out.println("Normal Iteration"); Iterator it1 = deque.iterator(); while (it1.hasNext()) { System.out.println(" "+ it1.next()); } // Reverse order iterator Iterator it2 = deque.descendingIterator(); System.out.println("Reversed Iteration"); while (it2.hasNext()) { System.out.println(" "+ it2.next()); } } }
Output
Elements of the deque[NodeJS, jQuery, Java, HTML5, AngualarJS, Javascript] First element of the deque before removal:NodeJS First element of the deque after removal:Java Last element of the deque before removal:Javascript Last element of the deque after removal:AngualarJS Standard Iterator Java HTML5 AngualarJS Reverse Iterator AngualarJS HTML5 Java
Leave a Reply