Home > Java > javaTutorial > body text

And in Java

WBOY
Release: 2024-08-30 16:03:51
Original
880 people have browsed it

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

Deque que =new Linkedlist();
Copy after login

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();
Copy after login

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
Copy after login

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);
}
}
Copy after login

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.

And in Java

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.

The above is the detailed content of And in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!