Java Stack Methods

About This Index
This page is the Java Stack Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, so the output shown is the output you get.
Learn what a stack is on the Stack hub. Use this page when you need a specific method such as push(), pop(), or search(). All examples compile on Java 8 and later.
java.util.Stack extends Vector, so it inherits dozens of methods. The first table lists the five LIFO methods that Stack declares. The other three tables are inherited from Vector and the List interface: they work by index, where index 0 is the bottom and size() - 1 is the top.
How to Call a Stack Method
Every method here is an instance method, so you call it on your stack variable:
import java.util.Stack;
Stack<String> stack = new Stack<>();
stack.push("a"); // [a]
stack.push("b"); // [a, b] b is the top
String top = stack.peek(); // "b"
String gone = stack.pop(); // "b", stack is [a]
String bottom = stack.get(0); // "a" (inherited from Vector)
int count = stack.size(); // 1 Java Stack Methods Table
All method guides in this Java tutorial series (26 tutorials), grouped by purpose.
Stack operations (LIFO)
5 methodsThe five methods declared in Stack itself. They work only on the top: the last element pushed is the first one popped.
| Method | Description | Tutorial |
|---|---|---|
push() | Put an element on top of the stack and get it back. | Try it |
pop() | Remove the top element and return it. | Try it |
peek() | Look at the top element without removing it. | Try it |
empty() | Stack's own check: true when there is nothing left to pop. | Try it |
search() | How far an element is from the top (top = 1), or -1. | Try it |
Add and change (inherited from Vector/List)
6 methodsInherited from Vector and the List interface. They can append, insert below the top, replace by index, or resize the stack.
| Method | Description | Tutorial |
|---|---|---|
add() | Append at the top (returns true) or insert at an index. | Try it |
addAll() | Push every element of a collection in its iteration order. | Try it |
insertElementAt() | Insert below the top: arguments are (element, index). | Try it |
set() | Replace the element at an index and return the old one. | Try it |
setElementAt() | Replace by index with arguments (element, index); returns nothing. | Try it |
setSize() | Grow with null elements or cut elements off the top. | Try it |
Read and search (inherited from Vector/List)
10 methodsInherited from Vector and List. Read by index (0 = bottom), look at either end, find positions, count, or copy to an array.
| Method | Description | Tutorial |
|---|---|---|
get() | Read the element at an index; index 0 is the bottom. | Try it |
elementAt() | Vector's legacy name for reading by index. | Try it |
firstElement() | Read the bottom element (the oldest one). | Try it |
lastElement() | Read the top element; throws NoSuchElementException if empty. | Try it |
indexOf() | Position of the first match, counted from the bottom. | Try it |
lastIndexOf() | Position of the match closest to the top. | Try it |
contains() | Check whether any element equals a value. | Try it |
size() | Count the elements on the stack. | Try it |
isEmpty() | The Collection-wide emptiness check. | Try it |
toArray() | Copy the elements, bottom to top, into an array. | Try it |
Remove (inherited from Vector/List)
5 methodsInherited from Vector and List. Remove by index, by value, or everything at once without popping one by one.
| Method | Description | Tutorial |
|---|---|---|
remove() | Remove by index (returns the element) or by value (returns boolean). | Try it |
removeElement() | Remove the first match from the bottom; returns boolean. | Try it |
removeElementAt() | Delete the element at an index; returns nothing. | Try it |
removeAllElements() | Vector's legacy way to empty the stack. | Try it |
clear() | Remove every element through the List API. | Try it |
Frequently Asked Questions
Start with push()
Put your first elements on a stack and see where the top is.
12 people found this page helpful
