In this tutorial, we will learn about Deque Interface in Java and some of its implementation classes and their usage.
Deque interface in Java is present in java.util package, which represents a queue of elements in which elements will be inserted and removed from both rear and front.
In other words, in a simple Queue, elements are inserted at the end of the queue and removed from the beginning of the queue (FIFO order). However, in a Deque, we can insert and remove elements from both the front and rear.
The Deque interface in Java extends the Queue interface and is a subtype of the Java Collection framework which means it inherits properties of the collection framework as well.
How to Declare Deque in Java?
Let’s see how to use Deque in Java:
Deque<T> deque1 = new ArrayDeque<>();
Here, “T” indicates the type of a Deque. For example,
// Array implementation of Deque
Deque<String> deque1 = new ArrayDeque<>();
// LinkedList implementation of Deque
Deque<String> deque2 = new LinkedList<>();
Deque Interface in Java
Below is the declaration of the Deque Interface present in JDK.
package java.util;
public interface Deque<E> extends Queue<E> {
}
Deque interface extends Queue interface, therefore, it leverages the functionality of Queue as well. It overrides few methods of Queue interface such as add(E e), remove(), poll(), peek(), etc
Java Deque Methods
Below are some commonly used Deque methods:
Methods | Description |
addFirst() | Inserts the element at the beginning of the deque. Throws an exception if the deque is full. |
addLast() | Inserts the element at the end of the deque. Throws an exception if the deque is full. |
removeFirst() | Returns and removes the first element of the deque. Throws an exception if the deque is empty. |
removeLast() | Returns and removes the last element of the deque. Throws an exception if the deque is empty. |
getFirst() | Returns the first element of the deque. Throws an exception if the deque is empty. |
getLast() | Returns the last element of the deque. Throws an exception if the deque is empty. |
peekFirst() | Returns the first element of the deque. Returns null if the deque is empty. |
peekLast() | Returns the last element of the deque. Returns null if the deque is empty. |
offerFirst() | Inserts the element at the beginning of the deque. Returns false if the deque is full. |
offerLast() | Inserts the element at the end of the deque. Returns false if the deque is full. |
pollFirst() | Returns and removes the first element of the deque. Returns null if the deque is empty. |
pollLast() | Returns and removes the last element of the deque. Returns null if the deque is empty. |
Java Deque Example:
Below is a sample program of Deque in Java, using ArrayDeque.
public class DequeExample {
public static void main(String[] args) {
// Creating Deque using the ArrayDeque class
Deque<Integer> deque1 = new ArrayDeque<>();
// Adding elements to the Deque
deque1.addFirst(1); // [1]
deque1.addFirst(2); // [2, 1]
deque1.addLast(3); // [2, 1, 3]
System.out.println("Printing Deque: " + deque1);
System.out.println("First Element: " + deque1.peekFirst());
System.out.println("Last Element: " + deque1.peekLast());
// Adding Elements using Offer Methods
deque1.offerFirst(4); // [4, 2, 1, 3]
deque1.offerLast(5); // [4, 2, 1, 3, 5]
System.out.println("Printing Deque: " + deque1);
}
}
Output:
Printing Deque: [2, 1, 3]
First Element: 2
Last Element: 3
Printing Deque: [4, 2, 1, 3, 5]
That’s it for the post, I hope you understand What is Deque Interface in Java? and where you can use it. You can read further posts for a better understanding of its implementations.
For Official Docs: Read Here