Java Deque

In this tutorial, we will learn about what is java.util.Deque, how it is different from other Queue's and use cases of it along with working examples.

java.util.Deque interface

In Java, java.util.Deque is an interface which extends the java.util.Queue and declares its own methods sets guidelines how to implement those methods for implementing Classes. This represents a sequence of elements that can be accessed in either direction of the Queue in short Dequeue is a "Double Ended Queue".

The Deque interface provides methods to insert, retrieve, and remove elements at both ends of the queue. It also provides methods to access the first and last elements of the queue without removing them.

Methods of a deque interface

  1. Allows the insertion, retrieval, and removal of elements at both ends of the queue.
  2. Provides methods to retrieve the first and last elements of the queue without removing them.
  3. This is implemented by classes such as ArrayDeque and LinkedList.
  4. Allows null elements to be inserted into the deque.

Deque interface example:

Here is an example of using the Deque interface with the ArrayDeque class:

CopiedCopy Code
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
    public static void main(String[] args) {
        // Create a new Deque
        Deque<String> deque = new ArrayDeque<>();
        // Add elements to the front of the Deque
        deque.addFirst("element 1");
        deque.addFirst("element 2");
        deque.addFirst("element 3");
        System.out.println("Deque after adding elements to front: " + deque);
        // Add elements to the back of the Deque
        deque.addLast("element 4");
        deque.addLast("element 5");
        deque.addLast("element 6");
        System.out.println("Deque after adding elements to back: " + deque);
        // Retrieve the first and last elements of the Deque
        System.out.println("First element: " + deque.getFirst());
        System.out.println("Last element: " + deque.getLast());
        // Remove elements from the front of the Deque
        deque.removeFirst();
        deque.removeFirst();
        System.out.println("Deque after removing elements from front: " + deque);
        // Remove elements from the back of the Deque
        deque.removeLast();
        deque.removeLast();
        System.out.println("Deque after removing elements from back: " + deque);
    }
}

Output:

Deque after adding elements to front: [element 3, element 2, element 1]
Deque after adding elements to back: [element 3, element 2, element 1, element 4, element 5, element 6]
First element: element 3
Last element: element 6
Deque after removing elements from front: [element 1, element 4, element 5, element 6]
Deque after removing elements from back: [element 1, element 4]

Explanation:

In the above example, we create a new ArrayDeque object and referenced it to Dequeue and added elements to both the front and back of the deque using the addFirst() and addLast() methods.

We then retrieve the first and last elements of the deque using the getFirst() and getLast() methods. Finally, we remove elements from both the front and back of the deque using the removeFirst() and removeLast() methods.

Conclusion:

In this tutorial, we have covered what is Deque interface and how to allows to add elements at both end of the Queue along with the working example of the Deque using its implemented class ArrayDeque.