In Java, push(e) puts an element on top of a stack. An ArrayDeque keeps the top of the stack at its head, so push(e) is exactly the same as addFirst(e). It returns void.
Pair it with pop() to remove the top and peek() to read it. The last element pushed is the first one popped: last in, first out (LIFO).
An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.
Syntax
Call push with the element to place on top.
Java
Deque<String> stack = new ArrayDeque<>();
stack.push("a"); // [a]
stack.push("b"); // [b, a] b is on top
Call
What it does
stack.push(e)
Puts e on top (at the head).
stack.addFirst(e)
Exactly the same operation.
stack.peek()
Reads the top without removing it.
stack.pop()
Removes and returns the top.
The Java documentation recommends Deque implementations such as ArrayDeque over the legacy java.util.Stack class. Note one visible difference: printing an ArrayDeque stack lists the top first, while printing a Stack lists the bottom first.
Library declarations
Java
// java.util.ArrayDeque<E>
public void push(E e)
// Declared in Deque<E>
Parameters
Parameter
Description
e
Required. The element to push. Must not be null.
Return Value
Return Value
Description
void
Nothing is returned. The element is now on top.
Throw or Return a Special Value?
Each end has a method that throws when it cannot work (such as on an empty deque) and one that returnsnull or false instead. The highlighted cell matches push().
Push one by one. plate1 goes in first, and each later plate is placed on top of the previous one.
Top is printed first. The deque prints from the head, which holds the top, so the output starts with plate3.
Read the top.peek() returns plate3, the most recent push.
The bottom is the tail.peekLast() reads the tail, which is plate1, the oldest element.
Example 2 — Reverse a Word with push and pop
Push each character, then pop them all. Popping returns them in the opposite order.
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequePushReverse {
public static void main(String[] args) {
String text = "Java";
Deque<Character> stack = new ArrayDeque<>();
for (char c : text.toCharArray()) {
stack.push(c);
}
System.out.println(stack);
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
System.out.println(sb);
}
}
Output
[a, v, a, J]
avaJ
How It Works
Characters as objects. Each char is boxed into a Character, since collections only store objects.
Last letter on top. After pushing J, a, v, a, the head holds the final a, so the deque prints [a, v, a, J].
Pop until empty. Each pop() removes the top character and sb.append adds it to the builder.
Reversed text. The builder ends up holding avaJ, the word backwards.
Example 3 — push Is addFirst, and null Is Refused
Mix push and addFirst to see they go to the same place, then try to push null.
Same end.push("x") and then addFirst("y") both insert at the head, so y sits in front of x.
Interchangeable names. Inside ArrayDeque, push just calls addFirst, so mixing them is safe, though sticking to one name reads better.
null on top is not allowed.push(null) throws NullPointerException.
Still two elements. The failed push did not change the stack, so the size stays 2.
Frequently Asked Questions
It inserts the element at the head of the deque, which is the top of the stack. It returns nothing.
Yes. ArrayDeque.push(e) is equivalent to addFirst(e).
Stack is a legacy, synchronized class built on Vector. ArrayDeque is not synchronized and is usually faster, and the Java documentation recommends Deque implementations for stacks.
Printing goes from head to tail, and push() inserts at the head, so the most recently pushed element is printed first.