Deque.push()
Pushes an element onto the top of a stack (the head of a deque).
Available since: Java 1.6
Syntax
void push(E e)Parameters
| Parameter | Type | Description |
|---|---|---|
| e | E | The element pushed onto the top of the stack. |
Examples
The last pushed element comes first:
void main() {
Deque<String> history = new ArrayDeque<>();
history.push("home");
history.push("catalog");
history.push("product");
IO.println(history);
IO.println(history.peek());
}Output:
[product, catalog, home]
productNotes
push(e) is the same as addFirst(e). Together with pop() and peek() it forms a last-in, first-out stack.
See also
- Deque.pop() — Removes and returns the element at the top of a stack.
- Queue.peek() — Returns the head of a queue without removing it.
- ArrayDeque — A fast array-based double-ended queue — a replacement for `Stack` and `LinkedList`.
Related recipes
In the tutorial
- Queues and deques — Queues in Java: Queue and ArrayDeque, a stack built on Deque, the offer, poll and peek methods and the PriorityQueue.