Deque in Java is a double-ended queue. It is used to add or remove data elements from either head or tail. Java.util.Dequeue interface is used by deque in Java which is a subtype of the java.util.Queue interface. Let us study the concept in detail.
Below mentioned pointers are going to be the agenda for this article:
Let’s begin!
What is Deque in Java?
The Deque is a double-ended queue. It helps in adding and removing data elements from a data structure from either head or tail. It can be used either as a FIFO or a LIFO. Examples of FIFO and LIFO are Queue and Stack respectively.
This is how the working diagrammatically looks like. Moving on, we have several methods included in the deque. Let’s take a look.
Methods used in the deque
Methods used in deque are as follows:
- addFirst(element): this method adds an element to the head.
- addLast(element): this method adds an element to the tail.
- add(element): this method adds an element to the tail.
- removeFirst(): this method removes the element from the head.
- removeLast(): this method removes the element from the tail.
- push(element): this method adds an element to the head.
- pop(element): this method removes an element from the head.
- offerFirst(element): this method adds an element to the head and will return a Boolean to depict if the insertion was successful.
- offerLast(element): this method removes an element from the tail and will return a Boolean to depict if the insertion was successful.
- descendingIterator(): this method returns an iterator that has the reverse order for this deque.
- poll(): this method retrieves and removes the head of the deque or it will return null if the deque is empty.
- pollFirst(): this method retrieves and removes the first element of this deque, or it will return null if this deque is empty.
- pollLast(): this method retrieves and removes the last element of this deque, or will return null if this deque is empty.
- iterator(): this method returns an iterator for the deque.
Well, these were a few methods. Let me show you the implementation process through a Java code.
Java program to show the working of deque
Take a look at the example program below:
import java.util.ArrayList; import java.util.List; public class DoubleEndedQueueImpl { private List<Integer> deque = new ArrayList<Integer>(); public void insertFront(int item){ //add element at the beginning of the queue System.out.println("element added at front: "+item); deque.add(0,item); System.out.println(deque); } public void insertRear(int item){ //add element at the end of the queue System.out.println("element added at rear: "+item); deque.add(item); System.out.println(deque); } public void removeFront(){ if(deque.isEmpty()){ System.out.println("Deque underflow, unable to remove."); return; } //remove an item from the beginning of the queue int rem = deque.remove(0); System.out.println("element removed from front: "+rem); System.out.println(deque); } public void removeRear(){ if(deque.isEmpty()){ System.out.println("Deque underflow, unable to remove."); return; } //remove an item from the beginning of the queue int rem = deque.remove(deque.size()-1); System.out.println("element removed from front: "+rem); System.out.println(deque); } public int peakFront(){ //gets the element from the front without removing it int item = deque.get(0); System.out.println("Element at first: "+item); return item; } public int peakRear(){ //gets the element from the rear without removing it int item = deque.get(deque.size()-1); System.out.println("Element at rear: "+item); return item; } public static void main(String a[]){ DoubleEndedQueueImpl deq = new DoubleEndedQueueImpl(); deq.insertFront(34); deq.insertRear(45); deq.removeFront(); deq.removeFront(); deq.removeFront(); deq.insertFront(21); deq.insertFront(98); deq.insertRear(5); deq.insertFront(43); deq.removeRear(); } }
Output
adding at front: 34 [34] adding at rear: 45 [34, 45] removed from front: 34 [45] removed from front: 45 [] Deque underflow!! unable to remove. adding at front: 21 [21] adding at front: 98 [98, 21] adding at rear: 5 [98, 21, 5] adding at front: 43 [43, 98, 21, 5] removed from front: 5 [43, 98, 21]
With this, we have reached towards the end of this article. I hope the content explained above added value to your Java knowledge. Keep reading, keep exploring!
Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this “Deque in Java” blog and we will get back to you as soon as possible.