Updated April 7, 2023
Definition of Deque in Java
Deque is an interface that is present in java. util package; basically it is a subtype of queue interface. Normally deque means double-ended queue, which means we can perform the insertion and deletion operation from both ends that are front and rear. In data structure deque we can consider it as a queue (first in first out, data structure) or we can consider it as a stack (last in first out, data structure). In deque, we cannot create objects because deque is an interface so we always need to create a class. Deque provides a better option as compared to other queue types and it has more advantages.
Syntax:
Deque que =new Linkedlist();
Explanation
We first need to create instances of the class to implement deque, so here we created a new instance of that LinkedList as shown in the above syntax. We can also create deque by using an array as follows.
Deque que =new ArrayDeque();
Explanation In the above syntax, we created an instance of a class by using an array that is Arraydeque as shown in the above syntax.
How did Deque work in Java?
Now let’s see how deque works in Java as follows. Normally in the queue, we can add elements from the rear end and we can remove elements from the front end but in deque, we can perform both operations from both ends in the deque. In Java Deque is an interface you need to launch a solid execution of the interface to utilize it. You can pick between the accompanying Deque executions in the Java Collections API:
java.util.LinkedList
java.util.ArrayDeque
The LinkedList class is a beautiful standard Deque and Queue execution. It utilizes a connected rundown inside to show a line or a deque.
The Java ArrayDeque class stores its components inside in a cluster. In the event that the quantity of components surpasses the space in the cluster, another exhibit is dispensed, and all components moved over. As such, the ArrayDeque develops on a case-by-case basis, regardless of whether it stores its components in an exhibit.
Methods of Deque
The Deque expands the Queue interface; it inherits every one of the strategies for the Queue interface.
Other than strategies accessible in the Queue interface, the Deque interface additionally incorporates the accompanying techniques:
- addFirst(): It is used to add the predefined component toward the start of the deque. Sometimes deque throws special cases if the deque is full.
- addLast(): It is used to add the predefined component toward the end of the deque. Sometimes deque throws a special case if the deque is full.
- offerFirst(): it is used to add the predefined component toward the start of the deque and sometimes it returns bogus if the deque is full.
- offerLast(): It is used to add the predetermined component toward the end of the deque and sometimes it returns bogus if the deque is full.
- getFirst(): Basically it is used to return the first component of the deque and if the deque is empty then it shows an exception that is deque is empty.
- getLast(): Basically it is used to return the last component of the deque and if the deque is empty then it shows an exception that is deque is empty.
- peekFirst(): Basically it is used to return the first component of the deque and if the deque is empty then it returns the null.
- peekLast(): Basically it is used to return the last component of the deque and if the deque is empty then it returns the null.
- removeFirst(): It is used to remove the first component of the deque and if the deque is empty then it shows an exception.
- removeLast(): It is used to remove the last component of the deque and if the deque is empty then it shows an exception.
- pollFirst(): Basically it is used to return the first component of the deque and if the deque is empty then it returns the null.
- pollLast(): Basically it is used to return the last component of the deque and if the deque is empty then it returns the null.
Deque as Stack Data Structure
The Stack class of the Java Collections system gives the execution of the stack.
Sometimes, it is prescribed to utilize Deque as a stack rather than the Stack class. Here are the techniques the Deque interface gives to carry out stack:
- push(): It is used to add a component toward the beginning of deque.
- pop(): It is used to remove a component from the beginning of deque.
- peek(): It is used to return a component from the beginning of deque.
Examples of Deque in Java
Now let’s see the difference of Deque in Java as follows.
import java.util.Deque;
import java.util.ArrayDeque;
class dque {
public static void main(String[] args) {
// creating Deque by using the ArrayDeque class as below
Deque<Integer> add = new ArrayDeque<>();
// Here we add values or we can say that component to the Deque
add.offer(5);
add.offerLast(4);
add.offerFirst(6);
System.out.println("Deque: " + add);
// Here access component from the Deque
int firstCompo = add.peekFirst();
System.out.println("First Component of Deque: " + firstCompo);
int lastCompo = add.peekLast();
System.out.println("Last Component of Deque: " + lastCompo);
// Here we remove component from the Deque
int revNum1 = add.pollFirst();
System.out.println("Removed First Component from the deque: " + revNum1);
int revNum2 = add.pollLast();
System.out.println("Removed last Component from the deque: " + revNum2);
System.out.println("Modified Deque is that: " + add);
}
}
Explanation
In the above example, we try to implement deque by using the ArrayDeque, in the above example, we try to insert the value at the first position and last position of deque as shown in the above example. Here we also access the deque value by using the peekLat () and pollFirst method as well as we also remove the value from the deque by using the pollFirst and pollLast() method. The end output of the code we illustrate by using the following screenshot.
The same way we can implement deque by using LinkedList.
Conclusion
We hope from this article you learn the Deque in Java. From the above article, we have learned the basic syntax of Deque in Java and we also see different examples of Deque. From this article, we learned how and when we use the Deque in Java.
Recommended Articles
This is a guide to Deque in Java. Here we discuss definition, syntax, How did Deque work in Java? examples with code implementation. You may also have a look at the following articles to learn more –