A Deque (double-ended queue) allows adding or removing elements from both ends and can work as either a FIFO queue or a LIFO stack.
- Supports FIFO and LIFO : Can function as a queue (FIFO) or a stack (LIFO).
- No Capacity Restriction (for most implementations) : Classes like ArrayDeque grow automatically as needed.
- More Versatile than Queue : Provides extra methods like addFirst(), addLast(), peekFirst(), peekLast().
- Efficient Insert/Remove at Both Ends : Constant-time performance for add/remove operations at either end in most implementations.
Example:
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class Geeks
{
public static void main(String[] args)
{
// Create a Deque of Strings
Deque<String> d = new ArrayDeque<>();
d.addFirst("1");
d.addLast("2");
String f = d.removeFirst();
String l = d.removeLast();
// Displaying the Deque
System.out.println("First: " + f + ", Last: " + l);
}
}
Hierarchy of Deque interface
Below is the hierarchy diagram of Queue Interface, where Deque Interface extends the Queue Interface. Go through the below diagram to know high level ideas on Deque Interface.
DequeSyntax: The Deque interface is declared as,
public interface Deque extends Queue
Creating Deque Objects
Since Deque is an interface, objects cannot be created of the type Deque. We always need a class that extends this list in order to create an object. And also, after the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the Deque. This type-safe Queue can be defined as:
Java
// Obj is the type of the object to be stored in Deque
Deque<Obj> Deque = new ArrayDeque<> ();
Example: Deque
Java
import java.util.*;
public class DequeExample {
public static void main(String[] args)
{
Deque<String> deque
= new LinkedList<String>();
// We can add elements to the queue in various ways
// Add at the last
deque.add("Element 1 (Tail)");
// Add at the first
deque.addFirst("Element 2 (Head)");
// Add at the last
deque.addLast("Element 3 (Tail)");
// Add at the first
deque.push("Element 4 (Head)");
// Add at the last
deque.offer("Element 5 (Tail)");
// Add at the first
deque.offerFirst("Element 6 (Head)");
System.out.println(deque + "\n");
// We can remove the first element or the last element.
deque.removeFirst();
deque.removeLast();
System.out.println("Deque after removing " + "first and last: " + deque);
}
}
Output[Element 6 (Head), Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail)]
Deque after removing first and last: [Element 4 (Head), Element 2 (Head), Element 1 (Tail)...
Deque Operations using ArrayDeque
Let’s see how to perform a few frequently used operations on the Deque using the ArrayDeque class.
1. Adding Elements
In order to add an element in a Deque, we can use the add() method. The difference between a Queue and a Deque is that in Deque, the addition is possible from any direction. Therefore, there are other two methods available named addFirst() and addLast() which are used to add the elements at either end.
Java
import java.util.*;
public class ArrayDequeDemo {
public static void main(String[] args)
{
// Initializing an deque
Deque<String> dq = new ArrayDeque<String>();
// add() method to insert
dq.add("For");
dq.addFirst("Geeks");
dq.addLast("Geeks");
System.out.println(dq);
}
}
Output[Geeks, For, Geeks]
2. Removing Elements
In order to remove an element from a Deque, there are various methods available.
- Since we can also remove from both ends, the Deque interface provides us with removeFirst(), removeLast() methods.
- This interface also provides us with the poll(), pop(), pollFirst(), pollLast() methods where pop() is used to remove and return the head of the Deque.
- However, poll() is used because this offers the same functionality as pop() and doesn't return an exception when the Deque is empty.
Java
import java.util.*;
public class ArrayDequeDemo {
public static void main(String[] args)
{
// Initializing an deque
Deque<String> dq = new ArrayDeque<String>();
// add() method to insert
dq.add("For");
dq.addFirst("Geeks");
dq.addLast("Geeks");
System.out.println(dq);
System.out.println(dq.pop());
System.out.println(dq.poll());
System.out.println(dq.pollFirst());
System.out.println(dq.pollLast());
}
}
Output[Geeks, For, Geeks]
Geeks
For
Geeks
null
3. Iterating through the Deque
Since a Deque can be iterated from both directions, the iterator method of the Deque interface provides us two ways to iterate. One from the first and the other from the back.
Java
import java.util.*;
public class ArrayDequeDemo {
public static void main(String[] args)
{
// Initializing an deque
Deque<String> dq = new ArrayDeque<String>();
// add() method to insert
dq.add("For");
dq.addFirst("Geeks");
dq.addLast("Geeks");
dq.add("is so good");
for (Iterator itr = dq.iterator(); itr.hasNext();) {
System.out.print(itr.next() + " ");
}
System.out.println();
for (Iterator itr = dq.descendingIterator(); itr.hasNext();) {
System.out.print(itr.next() + " ");
}
}
}
OutputGeeks For Geeks is so good
is so good Geeks For Geeks
Note: The class which implements the Deque interface is ArrayDeque.
ArrayDeque
ArrayDeque in the Java Collection Framework is a resizable array that allows adding or removing elements from both ends. - It has no capacity limit and grows as needed. It is not thread-safe, so multiple threads need external synchronization.
ArrayDeque is usually faster than Stack for stack operations and faster than LinkedList for queue operations.
Here’s how to create a queue using this class.
Java
import java.util.*;
public class ArrayDequeDemo {
public static void main(String[] args){
Deque<Integer> de_que = new ArrayDeque<Integer>(10);
// add() method to insert
de_que.add(10);
de_que.add(20);
de_que.add(30);
de_que.add(40);
de_que.add(50);
System.out.println(de_que);
// clear() method
de_que.clear();
// addFirst() method to insert the elements at the head
de_que.addFirst(564);
de_que.addFirst(291);
// addLast() method to insert the elements at the tail
de_que.addLast(24);
de_que.addLast(14);
System.out.println(de_que);
}
}
Output[10, 20, 30, 40, 50]
[291, 564, 24, 14]
Methods of Deque Interface
| Method | Description |
|---|
| void addFirst(E e) | Inserts element at the front; throws exception if full. |
| void addLast(E e) | Inserts element at the end; throws exception if full. |
| boolean offerFirst(E e) | Inserts at front; returns false if full. |
| boolean offerLast(E e) | Inserts at end; returns false if full. |
| E removeFirst() | Removes and returns first element; throws exception if empty. |
| E removeLast() | Removes and returns last element; throws exception if empty. |
| E pollFirst() | Removes and returns first element; returns null if empty. |
| E pollLast() | Removes and returns last element; returns null if empty. |
| E getFirst() | Retrieves first element without removing; throws exception if empty. |
| E getLast() | Retrieves last element without removing; throws exception if empty. |
| E peekFirst() | Retrieves first element without removing; returns null if empty. |
| E peekLast() | Retrieves last element without removing; returns null if empty. |
| boolean removeFirstOccurrence(Object o) | Removes first occurrence of the element. |
| boolean removeLastOccurrence(Object o) | Removes last occurrence of the element. |
| Iterator<E> descendingIterator() | Returns iterator from tail to head. |
| boolean add(E e) | Inserts at tail; throws exception if full. |
| boolean offer(E e) | Inserts at tail; returns false if full. |
| E remove() | Removes head; throws exception if empty. |
| E poll() | Removes head; returns null if empty. |
| E element() | Retrieves head; throws exception if empty. |
| E peek() | Retrieves head; returns null if empty. |
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java