Mastering Java's Deque Interface: Features, Methods, and Practical Examples
Java Deque Interface
Discover how Java’s Deque interface delivers a versatile double‑ended queue, and learn how to harness its powerful methods in real code.
How Deque Works
Unlike a conventional queue, which only enqueues at the rear and dequeues from the front, a Deque (double‑ended queue) lets you add and remove elements from both ends. This flexibility makes it ideal for LIFO, FIFO, and more complex data‑handling patterns.

Classes Implementing Deque
To work with a Deque, choose one of the following concrete implementations:
- ArrayDeque – a resizable array that offers fast, non‑synchronized access.
- LinkedList – a doubly linked list that supports null elements and provides robust insertion performance.

Using Deque in Your Code
First, import the interface:
import java.util.Deque;
Then instantiate a concrete class:
// Array implementation
Deque<String> animal1 = new ArrayDeque<>();
// LinkedList implementation
Deque<String> animal2 = new LinkedList<>();
These objects now expose all Deque operations.
Key Deque Methods
Because Deque extends Queue, it inherits all queue methods. In addition, it provides specialized operations for both ends:
- addFirst(e) – Inserts
eat the front, throwing an exception if the deque is full. - addLast(e) – Appends
eat the tail, throwing an exception if full. - offerFirst(e) – Attempts to insert at the front, returning
falseif full. - offerLast(e) – Attempts to append at the tail, returning
falseif full. - getFirst() / getLast() – Retrieves the first/last element, throwing an exception if the deque is empty.
- peekFirst() / peekLast() – Retrieves the first/last element, returning
nullwhen empty. - removeFirst() / removeLast() – Removes and returns the first/last element, throwing an exception when empty.
- pollFirst() / pollLast() – Removes and returns the first/last element, returning
nullif empty.
Deque as a Stack
The legacy Stack class is synchronized and less efficient. Deque offers a modern, unsynchronized alternative:
push(e)– Addseto the front.pop()– Removes and returns the front element.peek()– Retrieves the front element without removal.
Practical Example with ArrayDeque
import java.util.Deque;
import java.util.ArrayDeque;
class Main {
public static void main(String[] args) {
Deque<Integer> numbers = new ArrayDeque<>();
// Populate the deque
numbers.offer(1); // tail
numbers.offerLast(2); // tail
numbers.offerFirst(3); // head
System.out.println("Deque: " + numbers);
// Peek at both ends
System.out.println("First Element: " + numbers.peekFirst());
System.out.println("Last Element: " + numbers.peekLast());
// Remove elements
System.out.println("Removed First Element: " + numbers.pollFirst());
System.out.println("Removed Last Element: " + numbers.pollLast());
System.out.println("Updated Deque: " + numbers);
}
}
Output
Deque: [3, 1, 2] First Element: 3 Last Element: 2 Removed First Element: 3 Removed Last Element: 2 Updated Deque: [1]
For deeper insights, explore the official Java ArrayDeque documentation.
Java
- Mastering Java Interfaces: Concepts, Implementation, and Best Practices
- Java Collections Framework: Core Interfaces, Implementations, and Practical Usage
- Java Collection Interface: Core Concepts & Essential Methods
- Mastering Java’s Queue Interface: Methods, Implementations, and Practical Use
- Mastering Java ArrayDeque: Comprehensive Guide & Stack Implementation
- Java Map Interface – Comprehensive Guide to Map, Its Implementations, and Key Methods
- Java SortedMap Interface: Overview, Methods, and TreeMap Implementation
- Mastering Java NavigableMap: Features, Methods, and TreeMap Implementation
- Mastering Java’s ConcurrentMap: Thread‑Safe Maps Explained
- Mastering Java’s Set Interface: Concepts, Methods, and Practical Examples