Introduction
The Java Deque interface is one of the most flexible data structures in the Java Collection Framework. The term Deque stands for Double-Ended Queue, meaning elements can be inserted and removed from both the front and the rear of the collection.
Unlike a standard Queue, which follows the FIFO (First In, First Out) principle, a Deque supports both FIFO and LIFO (Last In, First Out) operations. This makes it suitable for implementing both queues and stacks.
The Deque interface belongs to the java.util package and extends the Queue interface. Java provides several implementations of Deque, including ArrayDeque and LinkedList.
What is Java Deque?
A Deque (Double-Ended Queue) is a collection that allows insertion and deletion of elements from both ends.
Unlike a normal Queue:
- Elements can be added at the front.
- Elements can be added at the rear.
- Elements can be removed from the front.
- Elements can be removed from the rear.
This flexibility makes Deque suitable for both queue and stack operations.
Key Characteristics
- Double-ended queue
- Supports FIFO and LIFO
- Dynamic size
- Allows duplicate elements
- Most implementations do not allow null values (for example, ArrayDeque)
- Supports Generics
- Efficient insertion and deletion at both ends
Java Deque Hierarchy
Iterable
│
Collection
│
Queue
│
Deque
│
├── ArrayDeque
└── LinkedList
Some concurrent implementations include:
- LinkedBlockingDeque
- ConcurrentLinkedDeque
Features of Java Deque
Some important features include:
- Double-ended insertion and deletion
- Can work as a Queue or Stack
- Dynamic resizing
- Supports Generics
- Allows duplicate elements
- Efficient operations at both ends
- Multiple implementations
- Rich collection methods
Syntax
Import Statement
import java.util.Deque;
import java.util.ArrayDeque;
or
import java.util.*;
Creating a Deque
Using ArrayDeque:
Deque<String> deque = new ArrayDeque<>();
Using LinkedList:
Deque<String> deque = new linkedlist<>();
Common Implementations of Deque
1. ArrayDeque
- Array-based implementation
- Faster than Stack and LinkedList in most cases
- Does not allow null elements
- Recommended for stack and queue operations
2. LinkedList
- Doubly linked list implementation
- Allows null elements
- Efficient insertion and deletion
- Can also be used as a Queue or List
Common Methods of Deque
| Method | Description |
|---|---|
| addFirst() | Inserts an element at the fron |
| addLast() | Inserts an element at the rear |
| offerFirst() | Inserts an element at the front safely |
| offerLast() | Inserts an element at the rear safelyt |
| removeFirst() | Removes the first elementt |
| removeLast() | Removes the last element |
| pollFirst() | Removes and returns the first element |
| pollLast() | Removes and returns the last element |
| peekFirst() | Returns the first element without removing it |
| peekLast() | Returns the last element without removing it |
| size() | Returns the number of elements |
| isEmpty()td> | Checks whether the deque is empty |
| clear() | Removes all elements |
Example 1: Creating a Deque
import java.util.*;
public class DequeExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.add("Java");
deque.add("Python");
deque.add("Spring Boot");
System.out.println(deque);
}
}
Output
Example 2: Adding Elements at Both Ends
import java.util.*;
public class AddExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.addFirst(20);
deque.addLast(40);
deque.addFirst(10);
deque.addLast(50);
System.out.println(deque);
}
}
Output
Example 3: Removing Elements
import java.util.*;
public class RemoveExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.add("A");
deque.add("B");
deque.add("C");
System.out.println(deque.removeFirst());
System.out.println(deque.removeLast());
System.out.println(deque);
}
}
Output
C
[B]
Example 4: Using peekFirst() and peekLast()
import java.util.*;
public class PeekExample {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
deque.add(100);
deque.add(200);
deque.add(300);
System.out.println(deque.peekFirst());
System.out.println(deque.peekLast());
}
}
Output
300
Example 5: Using Deque as a Stack
import java.util.*;
public class StackExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
stack.push("Java");
stack.push("Python");
stack.push("Spring");
while(!stack.isEmpty()){
System.out.println(stack.pop());
}
}
}
Output
Python
Java
Example 6: Using Deque as a Queue
import java.util.*;
public class QueueExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
queue.offerLast(10);
queue.offerLast(20);
queue.offerLast(30);
while(!queue.isEmpty()){
System.out.println(queue.pollFirst());
}
}
}
Output
20
30
Deque vs Queue
| Feature | Deque | Queue |
|---|---|---|
| Insert Front | Yes | No |
| Insert Rear | Yes | Yes |
| Remove Front | Yes | Yes |
| Remove Rear | Yes | No |
| FIFO Support | Yes | Yes |
| LIFO Support | Yes | No |
Deque vs Stack
| Feature | Deque | Queue |
|---|---|---|
| Double-ended operations | Yes | No |
| Performance | Faster (ArrayDeque) | Slower |
| Legacy Class | No | Yes |
| Recommended for New Projects | Yes | No |
ArrayDeque vs LinkedList
| Feature | ArrayDeque | LinkedList |
|---|---|---|
| Internal Structure | Dynamic Array | Doubly Linked List |
| Performance | Faster | Slightly Slower |
| Null Elements | Not Allowed | Allowed |
| Memory Usage | Lower | Higher |
Time Complexity
| Operation | Time Complexity |
|---|---|
| addFirst() | O(1) |
| addLast() | O(1) |
| removeFirst() | O(1) |
| removeLast() | O(1) |
| peekFirst() | O(1) |
| peekLast() | O(1) |
These constant-time operations make Deque an excellent choice for high-performance applications.
Real-life Example
Imagine you’re developing a music playlist application.
Users can:
- Add a new song to the beginning of the playlist.
- Add a song to the end.
- Remove the first song after it has been played.
- Remove the last song if they no longer want to hear it.
A Deque allows all these operations efficiently.
Other real-world applications include:
- Browser forward/back history
- Undo and redo functionality
- Sliding window algorithms
- Palindrome checking
- Cache implementation (LRU Cache)
- Task scheduling
- Expression evaluation
- Breadth-First Search (BFS)
- Depth-First Search (DFS)
Advantages of Java Deque
- Supports both FIFO and LIFO operations
- Efficient insertion and deletion from both ends
- Dynamic size
- Multiple implementations
- Supports Generics
- Faster than Stack in most scenarios
- Simple API
- Ideal for implementing queues and stacks
Limitations of Java Deque
- Cannot be instantiated directly because it is an interface
- Behavior varies depending on the implementation
- ArrayDeque does not allow null elements
- Not synchronized by default
- Random access is not supported efficiently
Common Mistakes
1. Instantiating Deque Directly
Incorrect:
Deque<Integer> deque = new Deque<>();
Correct:
Deque<Integer> deque = new array Deque<>();
2. Adding Null Elements to ArrayDeque
deque.add(null);
This throws a NullPointerException.
3. Confusing Queue and Deque
A Queue supports insertion at one end and removal at the other, while a Deque supports insertion and removal at both ends.
4. Using Stack Instead of Deque
For modern Java applications, ArrayDeque is generally recommended instead of the legacy Stack class because it provides better performance.
5. Forgetting Generics
Incorrect:
Deque deque = new ArrayDeque();
Correct:
Deque<string> deque = new array Deque<>();
Best Practices
- Program to the Deque interface.
Deque<string> deque = new array Deque<>();
- Prefer ArrayDeque for implementing stacks and queues.
- Use offerFirst() and offerLast() for safer insertion methods.
- Use pollFirst() and pollLast() instead of removeFirst() and removeLast() when empty collections are possible.
- Always use Generics for type safety.
- Choose LinkedList only when features specific to linked lists are required.
Conclusion
The Java Deque interface is a powerful and versatile component of the Java Collection Framework that supports efficient insertion and removal of elements from both ends of a collection. With implementations such as ArrayDeque and LinkedList, Deque can function as both a queue and a stack, making it suitable for a wide range of real-world applications.