new ArrayDeque<>() / deque.push() / pop()
| Since: | Java 5(2004) |
|---|
A Deque (Double Ended Queue) is a data structure that allows adding and removing elements from both ends. ArrayDeque can be used as a stack (last-in, first-out) or as a double-ended queue. Use push() and pop() for stack operations.
Syntax
// Creates an ArrayDeque. Deque<Type> dequeName = new ArrayDeque<>(); // Adds an element to the front (stack push). deque.push(element); // Removes and returns the front element (stack pop). deque.pop(); // Returns the front element without removing it. deque.peekFirst(); // Returns the last element without removing it. deque.peekLast();
Method List
| Method | Description |
|---|---|
| new ArrayDeque<>() | Creates an array-based double-ended queue. Can be used as a stack or queue. |
| push(E e) | Adds an element to the front. Equivalent to a stack push. |
| pop() | Removes and returns the front element. Equivalent to a stack pop. Throws an exception if the deque is empty. |
| peekFirst() | Returns the front element without removing it. Returns null if the deque is empty. |
| peekLast() | Returns the last element without removing it. Returns null if the deque is empty. |
Sample Code
DequePushPop.java
import java.util.ArrayDeque;
import java.util.Deque;
class DequePushPop {
public static void main(String[] args) {
// Used as a stack (last-in, first-out).
Deque<String> stack = new ArrayDeque<>();
stack.push("Ayanami Rei");
stack.push("Ikari Shinji");
stack.push("Soryu Asuka");
System.out.println(stack.peek()); // Prints "Soryu Asuka" (without removing).
System.out.println(stack.pop()); // Prints "Soryu Asuka" (removes it).
System.out.println(stack.pop()); // Prints "Ikari Shinji".
// Used as a double-ended queue.
Deque<Integer> deque = new ArrayDeque<>();
deque.addFirst(1); // Adds to the front.
deque.addLast(2); // Adds to the back.
deque.addFirst(0); // Adds to the front.
System.out.println(deque.peekFirst()); // Prints "0".
System.out.println(deque.peekLast()); // Prints "2".
System.out.println(deque.pollFirst()); // Prints "0" (removes it).
System.out.println(deque.pollLast()); // Prints "2" (removes it).
}
}
The command looks like this:
javac DequePushPop.java java DequePushPop Soryu Asuka Soryu Asuka Ikari Shinji 0 2 0 2
Common Mistakes
Common Mistake 1: Calling pop() on an empty Deque causes NoSuchElementException
pop() throws a NoSuchElementException when called on an empty Deque. It's like trying to launch an Eva unit with no pilot available — nothing is there to return.
PopEmptyNg.java
import java.util.ArrayDeque;
import java.util.Deque;
class PopEmptyNg {
public static void main(String[] args) {
Deque<String> stack = new ArrayDeque<>();
stack.push("Ayanami Rei");
stack.pop();
stack.pop();
}
}
The command looks like this:
javac PopEmptyNg.java java PopEmptyNg Exception in thread "main" java.util.NoSuchElementException
Like Ayanami Rei calmly assessing the situation, use poll() instead — it returns null on an empty Deque without throwing an exception. Alternatively, check with isEmpty() beforehand.
PopEmptyOk.java
import java.util.ArrayDeque;
import java.util.Deque;
class PopEmptyOk {
public static void main(String[] args) {
Deque<String> stack = new ArrayDeque<>();
stack.push("Ayanami Rei");
System.out.println(stack.poll());
System.out.println(stack.poll());
}
}
The command looks like this:
javac PopEmptyOk.java java PopEmptyOk Ayanami Rei null
Common Mistake 2: Adding a null element to ArrayDeque causes NullPointerException
ArrayDeque does not allow null elements. Attempting to push null throws a NullPointerException. Like Soryu Asuka having strict conditions for piloting, ArrayDeque only accepts non-null values.
DequeNullNg.java
import java.util.ArrayDeque;
import java.util.Deque;
class DequeNullNg {
public static void main(String[] args) {
Deque<String> stack = new ArrayDeque<>();
stack.push("Soryu Asuka");
stack.push(null);
}
}
The command looks like this:
javac DequeNullNg.java java DequeNullNg Exception in thread "main" java.lang.NullPointerException
Like Katsuragi Misato planning ahead, check for null before pushing, or use a LinkedList if null values are required.
DequeNullOk.java
import java.util.ArrayDeque;
import java.util.Deque;
class DequeNullOk {
public static void main(String[] args) {
Deque<String> stack = new ArrayDeque<>();
String pilot = "Soryu Asuka";
if (pilot != null) {
stack.push(pilot);
}
System.out.println(stack.peek());
}
}
The command looks like this:
javac DequeNullOk.java java DequeNullOk Soryu Asuka
Notes
When used as a stack, ArrayDeque is faster than the legacy Stack class. The Stack class extends Vector and carries synchronization overhead, so ArrayDeque is commonly used in single-threaded environments.
Internally, push() calls addFirst() and pop() calls removeFirst(). pop() throws a NoSuchElementException on an empty deque, so check with isEmpty() beforehand.
For queue operations (first-in, first-out), see 'new LinkedList<>() / offer() / poll() / peek()'.
If you find any errors or copyright issues, please contact us.