The Deque
(Double-Ended Queue) interface in Java is part of the java.util
package and provides a more flexible and efficient way to handle collections. It supports the insertion and removal of elements from both ends of the queue, which makes it versatile for implementing both FIFO (First-In-First-Out) and LIFO (Last-In-First-Out) behaviors.
Key Methods of Deque
addFirst(E e)
: Adds the specified element to the front of the deque.
Deque<Integer> deque = new ArrayDeque<>(); deque.addFirst(1); System.out.println(deque); // Output: [1]
addLast(E e)
: Adds the specified element to the end of the deque.
deque.addLast(2); System.out.println(deque); // Output: [1, 2]
offerFirst(E e)
: Adds the specified element to the front of the deque, returningtrue
upon success.
deque.offerFirst(0); System.out.println(deque); // Output: [0, 1, 2]
offerLast(E e)
: Adds the specified element to the end of the deque, returningtrue
upon success.
deque.offerLast(3); System.out.println(deque); // Output: [0, 1, 2, 3]
removeFirst()
: Removes and returns the first element from the deque.
int first = deque.removeFirst(); System.out.println(first); // Output: 0 System.out.println(deque); // Output: [1, 2, 3]
removeLast()
: Removes and returns the last element from the deque.
int last = deque.removeLast(); System.out.println(last); // Output: 3 System.out.println(deque); // Output: [1, 2]
pollFirst()
: Retrieves and removes the first element from the deque, or returnsnull
if the deque is empty.
int pollFirst = deque.pollFirst(); System.out.println(pollFirst); // Output: 1 System.out.println(deque); // Output: [2]
pollLast()
: Retrieves and removes the last element from the deque, or returnsnull
if the deque is empty.
int pollLast = deque.pollLast(); System.out.println(pollLast); // Output: 2 System.out.println(deque); // Output: []
getFirst()
: Retrieves, but does not remove, the first element of the deque.
deque.addFirst(10); System.out.println(deque.getFirst()); // Output: 10
getLast()
: Retrieves, but does not remove, the last element of the deque.
deque.addLast(20); System.out.println(deque.getLast()); // Output: 20
Why Use Deque
Over Stack
?
- Greater Flexibility:
Deque
supports both FIFO and LIFO operations, unlikeStack
, which is strictly LIFO. - Better Performance:
Deque
implementations likeArrayDeque
offer more efficient operations, providingO(1)
time complexity for adding/removing elements from both ends. - No Synchronization Overhead:
Stack
is synchronized, which can reduce performance in single-threaded scenarios.Deque
is not synchronized by default, offering better performance and flexibility. - Modern API:
Deque
offers more intuitive and useful methods likeofferFirst()
,offerLast()
, andpollFirst()
that provide better control over data management. - Integration with Collections Framework:
Deque
is a modern part of the Java Collections Framework and adheres to current Java design principles, unlike the legacyStack
.
Conclusion
In Java, Deque
is a more flexible, efficient, and modern alternative to the legacy Stack
class. It provides both FIFO and LIFO support with better performance and cleaner API methods. If you need a stack-like structure, using Deque
is generally a better choice.