Java/J2EE and SOA (326 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (21 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Deque in Java and how to implement its interface?

Last updated on Jul 21,2020 2.3K Views
8 / 22 Blog from Java Collections

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:

  1. What is Deque in Java?
  2. Methods used in Deque
  3. Java program to show the working of deque

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.Deque in Java

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:

  1. addFirst(element): this method adds an element to the head.
  2. addLast(element): this method adds an element to the tail.
  3. add(element): this method adds an element to the tail.
  4. removeFirst(): this method removes the element from the head.
  5. removeLast(): this method removes the element from the tail.
  6. push(element): this method adds an element to the head.
  7. pop(element): this method removes an element from the head.
  8. offerFirst(element): this method adds an element to the head and will return a Boolean to depict if the insertion was successful.
  9. offerLast(element): this method removes an element from the tail and will return a Boolean to depict if the insertion was successful.
  10. descendingIterator(): this method returns an iterator that has the reverse order for this deque.
  11. poll(): this method retrieves and removes the head of the deque or it will return null if the deque is empty.
  12. pollFirst(): this method retrieves and removes the first element of this deque, or it will return null if this deque is empty.
  13. pollLast(): this method retrieves and removes the last element of this deque, or will return null if this deque is empty.
  14. 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.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.