A bi-directional Java queue or bridge is a linear data structure that makes it possible to insert and remove elements at both ends (head and tail). It’s usually pronounced deck, not the ring.

Java deque is an extended version of the queue to handle two-way queues. It expands the queue interface with additional methods for adding and deleting items at both ends of the queue.

The Deque interface has recently been introduced in Java 1.6. It is now part of the framework of the Java collection. It is included in the java.util.Deque package.

A two-sided queue in Java can be used as a FIFO (first queue, first exit queue) or a LIFO (last queue, first exit queue). Adding items in the middle of the queue is not supported.

Java Deque Interface Hierarchy

The Java Deque interface expands the queue interface so that you can wait in both directions. It is implemented by the LinkedList and ArrayDeque classes, which can be used to create a queue that can grow as needed.

Java LinkedList is ideal for queues because it is efficient for adding and removing items at both ends of the list.

The other implementation classes are ConcurrentLinkedDeque and LinkedBlockingDeque, which also implement the Deque interface. A hierarchical scheme of the Deque-interface in Java is shown in the following figure.

Java Platform Interface Hierarchy Diagram

Bridge interface explanation

Deque is a generic interface that can be explained as follows:

Deque
‘s public interface expands the queue

Here E stands for the type of objects picked up by the bridge.

Features of the JavaDouble Ended Queue

Here are some interesting features of deque in Java :

1. The Java Deque interface organizes the elements of a first-in-first-out or last-in-first-out policy.

2. Deke doesn’t save zero points. If you try to add, a NullPointerException will be thrown.

3. The main functions of Deque are Push and Pop. The push() and pop() methods are often used to include Deque in a stack function. To place an element on the stack, call up the push( ) method. To remove the top element, call the pop( ) method.

4. Items can be picked up and removed at either end of the queue.

5. Items can be added from both ends of the queue.

6. The LinkedList and ArrayDeque classes are two implementation classes for the Deque interface.

7. The ArrayDeque class can be implemented when the Deque is used as a LIFO queue (or stack).

8. Implementing LinkedList works best when the deque is used as a FIFO queue (or just as a queue).

Deca methods in Java

In addition to the methods inherited from the Collections and Queue interfaces, the Deque interface adds 17 new methods to facilitate insertion, deletion and viewing on both sides. They’re like this:

1. Cancel addFirst(E) : This method is used to add an element to the bridgehead. An exception called Illegal StateChain is launched when the dean with limited capacity gets out of space.

2. cancel addLast(E) : This method is used to add an element at the end of the deck. A Forbidden State Acceptance is launched if there is a bridge with limited space capacity.

3rd logical setFirst(E) : It is used to add an element to the bridgehead. Returns where if the item has been successfully added to the checkout, otherwise it returns falsely. However, this is not an exception to the rule.

4. Boolean sentenceLast : Used to add an element at the end of the bridge. Returns where if the item has been successfully added to the checkout, otherwise it returns falsely. However, this is not an exception to the rule.

5. E removeFirst() : This method is used to remove and delete the head element from the block. You throw an exception called NoSuchElementException when the bridge is empty.

6. E removeLast() : This method is used to remove and remove an element from the end of the bridge. You throw an exception called NoSuchElementException when the bridge is empty.

7. E pollFirst() : The pollFirst() method performs the same task as the removeFirst() method. However, it returns zero if the disk is empty.

8. E pollLast() : The pollLast() method performs the same task as the removeLast() method. However, it returns zero if the disk is empty.

9. E getFirst() : Used to unpack without removing the first element from the cover. A NoSuchElementException is launched when the bridge is empty.

10. E getLast() : Used to remove the last element from the end of the terrace without extracting it. A NoSuchElementException is launched when the bridge is empty.

11. E peekFirst() : The peekFirst() method works with the same task as the getFirst() method. It returns a zero object when the die is empty instead of throwing an exception.

12. E peekLast() : The peekLast() method works with the same task as the getLast() method. It returns a zero object when the die is empty instead of throwing an exception.

13. invalid push(E ) : The push() method adds (or pushes) an element to the block head. An Illegal State Acceptance is triggered when there is no more room in Deque’s limited capacity. This method is similar to the addFirst() method.

14. E pop() : The pop() method removes (or pop) the element from the deco header. When the bridge is empty, a NoSuchElementException is started. This method is similar to the deleteFirst() method.

15. boolean removeFirstOccurrence(Object o) : This method removes the first occurrence of the specified element on the bridge. Displays the veracity if the removal operation was successful, and false if the deck does not contain the specified item.

16. boolean deleteLastOccurrence(Object o) : This method removes the last occurrence of the specified element on the bridge. It shows the truth if the removal operation was successful, and false if the deck did not contain the specified item.

17. Iterator down() : Returns an iterator object on its elements in reverse order (from end to head). The descendantIterator() method works in the same way as the iterative() method, except that it moves in the opposite direction.

Java DequeImplementation examples

Let’s take an example program where we will perform operations based on the getFirst(), removeFirst(), addFirst(), getLast(), removeLast() and addLast() methods. Let’s implement the ArrayDeque class for the Deque interface.

Program source code 1 :

Import java.util.ArrayDeque ;
Import java.util.Deque ;

public class DequeTestEx [
public static void head (String[] args)
[
// Create a Deque and add elements to Decks.
Deque dq = new ArrayDeque() ;

dq.aanbieding(ABC);
dq.aanbieding(PQR);
dq.aanbieding(MNO);
dq.aanbieding(IJK);
dq.aanbieding(GHI) ;

System.out.println(dq) ;

System.out.println(dq.getFirst() : +dq.getFirst()) ;
System.out.println(dq) ;

System.out.println(dq.removeFirst(): +dq.removeFirst());
System.out.println(dq) ;

// Add a new item to the head of the queue.
dq.addFirst(ABC) ;
System.out.println(dq) ;

System.out.println(dq.getLast() : +dq.getLast()) ;
System.out.println(dq) ;

System.out.println(dq.removeLast(): +dq.removeLast());
System.out.println(dq) ;

dq.addLast(GHI);
System.out.println(dq);
}
}

Go away:

dq.getFirst(): ABC
[ABC, PQR, MNO, IJK, GHI] dq.removeFirst() : ABC
[PQR, MNO, IJK, GHI] [ABC, PQR, MNO, IJK, GHI] dq.getLast() : GHI
[ABC, PQR, MNO, IJK, GHI] dq.removeLast() : GHEE [ABC, PQR, MNO, CALIBRATION] [ABC, PQR, MNO, CALIBRATION, GHEE].

Implementation of Deque to Java with LinkedList

Let’s create a program in which we use deque as a FIFO queue (or just a queue). For the Deque interface we implement the LinkedList class. The LinkedList class works best if we use it as a FIFO queue. Please refer to the source code for a better understanding.

Program source code 2 : Use the bridge as a FIFO queue.

Import java.util.Deque;
Import java.util.LinkedList;
Import java.util.NoSuchElementException ;

public class DequeAsQue [
public static void main(String[] args)
[
// Create a Deque and at the end add elements using the addLast() or offerLast() method
Deque dq = new LinkedList<>() ;

dq.addLast(John);
dq.offerLast(Richard);
dq.offerLast(Donna);
dq.offerLast(Ken);
dq.offer(Peter) ;

System.out.println(Deque: + dq) ;

// Remove elements from dq until empty.
while (dq.peekFirst() != null) {
System.out.println(Head Element: + dq.peekFirst());
System.out.println(Remove Element from Deque: +dq.removeFirst());
System.out.println(Elements from deque: + dq);
}
System.out.println(n);
// Now deque is empty. Try to call its methods peekFirst(), getFirst(), pollFirst() and removeFirst().

System.out.println(deque.isEmpty(): + dq.isEmpty());
System.out.println(deque.peekFirst(): + dq.peekFirst());
System.out.println(deque.pollFirst(): + dq.pollFirst()));
try {
String str = dq.getFirst();
System.out.println(deque.getFirst()): + str);
}
catch (NoSuchElementException e) {
System.out.println(deque.getFirst(): deque is empty.);
}
try {
String str = dq.removeFirst();
System.out.println(deque.removeFirst(): + str);
}
catch (NoSuchElementException e) {
System.out.println(deque.removeFirst(): deque is empty.);
}
}
}

Get out of here:
Deke: (John, Richard, Donna, Ken, Peter)
Main Element: JohnElement removed from Deke: JohnElements in Dec: (Richard, Donna, Ken, Peter)
Main Element: Richard Deke’s deleted: Richard
items in dec: (Donna, Ken, Peter)
Main Element: Item away from Deke: DonnaElements in Dec: Main element
: KenElement removed from Deke: KenElements in Dec: Main Component : PeterDelete Deke’s article: PeterElements in December: [].

deque.isEmpty() : real
deque.peekFirst() : null
deque.pollFirst() : null
deque.getFirst() : The deque is empty.
deque.removeFirst() : Deke’s empty.

Version Deque in Java with ArrayDeque

Let’s create another program in which we demonstrate how to use Deque as a stack (or LIFO queue). Let’s implement the ArrayDeque class for the Deque interface.

Program source code 3 : Use Deque as a stack (or LIFO queue).

Import java.util.ArrayDeque ;
Import java.util.Deque ;

public class DequeAsStack [
public static void main(String[] args)
[
// Create a Deque and use it as a stack. At the end, add elements using the addLast() or offerLast() method.
Deque dq = new ArrayDeque<>() ;

dk.flush(John); dk.flush(Richard); dk.flush(Donna); dk.flush(Ken); dk.flush(Peter);

System.out.println(stack: + dq) ;

// Remove all elements from dq.while (dq.peek() != null) {System.out.println(Element up: + dq.peek());System.out.println(Opened: + dq.pop());System.out.println(Stack: + dq);}System.out.println( Is stack empty: + dq.isEmpty());}}}.

Get out of here:
Pile: (Peter, Ken, Donna, Richard, John)
top item: PeterGot it: PeterStack: (Ken, Donna, Richard, John)
top item: KenPopping: KenStack:
top item: DonnaPopping: DonnaStack: Point
above: RichardPopping: Richard Stack: The element at the top: JohnPopp: JohnStack:
The battery’s dead, that’s right.

How can I play on the terrace in Java?

Let’s take the example of a program in which we iterate on Deque-elements using the iterator() method. The iterator method() returns an iterator object that moves over the bridge elements in LIFO form, i.e. the elements are moved from head to tail.

Program source code 4 :

Import java.util.ArrayDeque;
Import java.util.Deque;
Import java.util.Iterator ;

public class IteratingDeque [
public static void head(String[] args)
[
Deque dq = new ArrayDeque() ;

dq.offer(50) ;
dq.offer(10) ;
dq.offer(20) ;
dq.offer(05) ;
dq.offer(30) ;

System.out.println(Items on file :);
System.out.println(dq) ;

// Iteration on elements dec/
System.out.println(nIteration forward :);
Iterator itr = dq.iterator() ;
while(itr.hasNext()){
System.out.println(itr.next())) ;
}
// Switch elements in reverse order.
System.out.println(nIteration in backward:)));
Iterator itr2 = dq.descendingIterator();
while(itr2.hasNext()){
System.out.println(itr2.next()));
}
}
}.

Get out of here:
The elements of the bridge:
[50, 10, 20, 5, 30]

Iteration towards the future:
50
10
20
5
30

Iteration in reverse order:
30
5
20
10
50

Let’s take another example of a program in which we use an extended loop to iterate the elements of Deque in LIFO order. Look at the source code.

Program source code 5 :

import java.util.ArrayDeque ;
import java.util.Deque ;
public class DequeAsQue [
public static void main(String[] args)
[
Deque dq = new ArrayDeque() ;

dq.offer(50) ;
dq.offer(10) ;
dq.offer(20) ;
dq.offer(05) ;
dq.offer(30) ;

// Iterative elements of this with the improved for the loop.
System.out.println(Iterate with extended for loop);
for (Integer element: dq) {
System.out.println(element);
}
}
}

Get out of here:
Iteration with reinforced loop
50
10
20
5
30

I hope this tutorial has covered almost all the important points concerning the Deque-interface in Java with example programs. I hope you understand that.
Thanks for reading!

arraydeque vs linkedlist,java deque time complexity,java arraydeque,java dequeue hackerrank solution,java deque iterator remove,deque and queue are derived from mcq,what is the advantage of linked lists?,java util deque java 8,queue in java in javatpoint,enqueue java,different types of queue in java,use to remove or make the deque empty,deque and queue are derived from list,deque implementation python,deque iterator java,deque implementation in java using arrays,arraydeque api,deque c#,deque peek python,java qu,dequeue enqueue java,deque offer java,dequeue method java linked list,deque java time complexity,java deque comparator,returns the last element of the deque,convert arraydeque to arraylist,priority queue beginners book,priority queue in java,java collections beginners book,deque and queue are derived from collection,arraydeque java 8,arraydeque source code,array deque python,arraydeque cs61b,arraydeque add vs offer,java deque hackerrank solution,linkedlist in java programiz,arrayblockingqueue in java,java queue methods,java deque tutorialspoint,deque java implementation,deque implementation in java using linked list,deque java example,dequeue method java,queue in java,deque java 8,arraydeque in java

You May Also Like

14 Solutions To Fix Headphone Jack Not Working In Windows 10

We all plug headphones into our Windows 10 computers for entertainment and…

What is JDK | Java Platform (Ecosystem)

Java platform (ecosystem) There’s no reason not to. There’s no reason not…

How to Rotate and Flip a Video in VLC Media Player

VLC Media Player can do much more for videos than just play…

🥇 EXVAGOS Close?  How to enter? + Alternatives + List ▷ 2021

Exvagos, was considered one of the most popular file sharing portals in…