Java Deque
The Java Deque interface extends the Queue interface and is a double-ended queue. It provides methods to add, access, and remove items at the top and end of a deque. Thereby, it can be used as a queue or stack. The Deque interface is included in the java.util package.
Java Deque Syntax
import java.util.Deque;
Deque<DataType> myDeque = new DequeClass<DataType>();
In the syntax:
DataType: The data type to be stored in the deque.DequeClass: A class implementing theDequeinterface, e.g.,ArrayDequeorLinkedList.
Java Deque Methods
This list contains a selection of methods provided by the Java Deque interface:
.addFirst(item): Addsitemat the top of theDequeif possible, otherwise it throws an exception..addLast(item): Addsitemat the end of theDequeif possible, otherwise it throws an exception..getFirst(): Returns, without removal, the first item of theDeque. It throws an exception if the Deque is empty..offerLast(item): Addsitemat the end of theDequeif possible, otherwise it returnsfalse..peekFirst(): Returns, without removal, the first element of theDeque. Returnsnullif theDequeis empty..pollFirst(): Returns and removes the first item of theDeque. Returnsnullif theDequeis empty..removeFirst(): Returns and removes the first item of theDeque. Throws an exception if theDequeis empty.
Example 1: Implementing Java Deque Using LinkedList
This example shows how to implement Java Deque using LinkedList:
import java.util.Deque;import java.util.LinkedList;public class DequeExample {public static void main(String[] args) {Deque<String> deque = new LinkedList<>();deque.addFirst("Apple");deque.addLast("Banana");deque.addFirst("Orange");System.out.println("Deque: " + deque);deque.removeLast();System.out.println("After removing last: " + deque);}}
Here is the output:
Deque: [Orange, Apple, Banana]After removing last: [Orange, Apple]
Example 2: Implementing Java Deque Using ArrayDeque
This example shows how to implement Java Deque using ArrayDeque:
import java.util.Deque;import java.util.ArrayDeque;public class Main {public static void main(String[] args) {Deque<String> food = new ArrayDeque<String>();food.addFirst("Cabbage");food.addLast("Sausage");food.addFirst("Potatoes");food.addLast("Salad");System.out.println(food);System.out.println(food.pollFirst());System.out.println(food.peekFirst());System.out.println(food);}}
Here is the output:
[Potatoes, Cabbage, Sausage, Salad]PotatoesCabbage[Cabbage, Sausage, Salad]
Example 3: Using Java Deque as a Stack
This example shows how to use Java Deque as a stack:
import java.util.Deque;import java.util.ArrayDeque;public class StackExample {public static void main(String[] args) {Deque<Integer> stack = new ArrayDeque<>();stack.push(10);stack.push(20);stack.push(30);System.out.println("Stack: " + stack);System.out.println("Popped: " + stack.pop());System.out.println("After pop: " + stack);}}
Here is the output:
Stack: [30, 20, 10]Popped: 30After pop: [20, 10]
Frequently Asked Questions
1. Is Deque FIFO or LIFO?
Deque can behave as both FIFO (queue) and LIFO (stack) depending on the methods used.
2. Is a Deque faster than a Stack?
Yes, Deque (like ArrayDeque) is faster than Stack because Stack is synchronized, which adds overhead for single-threaded operations.
3. Is Java Queue FIFO or LIFO?
A Java Queue follows FIFO (First-In-First-Out) order, meaning elements are removed in the order they were added.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours