DeQue java example: The Deque is similar to the double-ended queue that helps to add or remove elements from each end of the data structure. In this tutorial, we will discover & understand completely what is Deque interface, declaration, working of java deque, how to create a deque in java using classes that implement it, and its methods with example programs.
This tutorial on Deque Interface in Java includes the following topics
- Java DeQue
- Deque Interface Declaration
- Working of Deque
- Classes that implement Deque
- Creating a Deque
- Java Deque Example
- Methods of Deque Interface in Java
- Deque Implementation In Java
- Deque as Stack Data Structure
- Implementation of Deque in ArrayDeque Class
Java DeQue
Java deque methods: Deque in Java is an interface present in java.util package. The Deque interface in Java was added by Java 6 version. It extends the Queue interface and declares the behavior of a double-ended queue. In Deque we can add or remove elements from both sides of the queue. The Deque can function as standard, first-in, first-out queues or as last-in, first-out stacks. The Deque doesn’t allow you to insert the null element.
Deque Interface Declaration
public interface Deque<E> extends Queue<E>
Do Check:
Working of Deque
Java deque implementation: Well in a normal queue, we will add elements from the rear and remove them from the front. But in a deque, we should insert and remove elements from both the rear and front.
Classes that implement Deque
If you want to use the Deque functionalities then you must add these two classes that implement Deque Interface. They are as follows:
- LinkedList
- ArrayDeque
Creating a Deque
Prior to using a deque in java, we should create an instance of one of the classes that implement the java deque interface. Let’s have a look at the example of creating a deque instance by creating a LinkedList interface or by creating an ArrayDeque instance:
// LinkedList implementation of Deque Deque deque = new LinkedList(); // Array implementation of Deque Deque deque = new ArrayDeque();
Java Deque Example
import java.util.*; class DequeExample{ public static void main(String args[]){ Deque dq = new LinkedList(); //adding the element in the deque dq.add("Ajay"); dq.add("Vijay"); dq.add("Rahul"); dq.addFirst("Amit"); dq.addLast("Sumit"); System.out.println("Deque elements are: " +dq); //remove last element System.out.println("remove last: " +dq.removeLast()); /*return the element at the head of a deque but not removed. It returns null if the deque is empty.*/ System.out.println("peek(): " +dq.peek()); //returns and remove the head element of the deque. System.out.println("poll(): " +dq.poll()); /* returns and remove the first element of the deque. It returns null if the deque is empty.*/ System.out.println("pollFirst(): " +dq.pollFirst()); //dispalying deque element System.out.println("After all operation deque elements are: " +dq); } }
Output:
Methods of Deque Interface in Java
1. add(E e): This method is used to insert a specified element to the tail.
2. addFirst(E e): This method is used to insert a specified element to the head.
3. addLast(E e): This method is used to insert the specified element to the tail.
4. E getFirst(): This method returns the first element in the deque.
5. E getLast(): This method returns the last element in the deque.
6. offer(E e): This method adds an element to the tail of the deque and returns a boolean value.
7. offerFirst(E e): This method adds an element to the head of the queue and returns a boolean value if the insertion was successful.
8. offerLast(E e): This method adds an element to the tail of the queue and returns a boolean value if the insertion was successful.
9. removeFirst(): It removes the element at the head of the deque.
10. removeLast(): It removes the element at the tail of the deque.
11. push(E e): This method adds the specified element at the head of the queue
12. pop(): It removes the element from the head and returns it.
13. poll(): returns and remove the head element of the deque.
14. pollFirst(): returns and remove the first element of the deque. It returns null if the deque is empty.
15. pollLast(): returns and removes the last element of the deque. It returns null if the deque is empty.
16. peek(): return the element at the head of a deque but not removed. It returns null if the deque is empty.
17. peekFirst(): return the first element at the head of a deque but not removed. It returns null if the deque is empty.
18. peekLast(): return the last element at the head of a deque but not removed. It returns null if the deque is empty.
Deque Implementation In Java
import java.util.*; public class Main { public static void main(String[] args) { //Declare Deque object Deque<String> deque = new LinkedList<String>(); // add elements to the queue using various methods deque.add("One"); //add () deque.addFirst("Two"); //addFirst () deque.addLast("Three"); //addLast () deque.push("Four"); //push () deque.offer("Five"); //offer () deque.offerFirst("Six"); //offerFirst () deque.offerLast("Seven"); //offerLast () System.out.println("Initial Deque:"); System.out.print(deque + " "); // Iterate using standard iterator System.out.println("\n\nDeque contents using Standard Iterator:"); Iterator iterator = deque.iterator(); while (iterator.hasNext()) System.out.print(" " + iterator.next()); // Iterate using Reverse order iterator Iterator reverse = deque.descendingIterator(); System.out.println("\n\nDeque contents using Reverse Iterator:"); while (reverse.hasNext()) System.out.print(" " + reverse.next()); // Peek () method System.out.println("\n\nDeque Peek:" + deque.peek()); System.out.println("\nDeque,After peek:" + deque); // Pop () method System.out.println("\nDeque Pop:" + deque.pop()); System.out.println("\nDeque,After pop:" + deque); // contains () method System.out.println("\nDeque Contains Three: " + deque.contains("Three")); deque.removeFirst(); //removeFirst () deque.removeLast(); //removeLast () System.out.println("\nDeque, after removing " + "first and last elements: " + deque); } }
Output:
Deque as Stack Data Structure
The implementation of a stack can be provided by the Stack Class of the Java Collections framework.
Yet, it recommends using Deque as a stack instead of the Stack class. It happens due to the methods of Stack are synchronized.
Following are the methods the Deque interface grants to implement stack:
- push() – Adds the specified element at the head of the queue
- pop() – Removes the element from the head and returns it.
- peek() – Return the element at the head of a deque but not removed. It returns null if the deque is empty.
Implementation of Deque in ArrayDeque Class Example
import java.util.Deque; import java.util.ArrayDeque; class Main { public static void main(String[] args) { // Creating Deque using the ArrayDeque class Deque<Integer> numbers = new ArrayDeque<>(); // add elements to the Deque numbers.offer(1); numbers.offerLast(2); numbers.offerFirst(3); System.out.println("Deque: " + numbers); // Access elements of the Deque int firstElement = numbers.peekFirst(); System.out.println("First Element: " + firstElement); int lastElement = numbers.peekLast(); System.out.println("Last Element: " + lastElement); // Remove elements from the Deque int removedNumber1 = numbers.pollFirst(); System.out.println("Removed First Element: " + removedNumber1); int removedNumber2 = numbers.pollLast(); System.out.println("Removed Last Element: " + removedNumber2); System.out.println("Updated Deque: " + numbers); } }
Output: