Java Stack Methods

Beginner
5 min read
Updated: Nov 2026
26 method guides
Method index

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.

Only five methods belong to Stack itself.

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:

Java
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 methods

The five methods declared in Stack itself. They work only on the top: the last element pushed is the first one popped.

MethodDescriptionTutorial
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 methods

Inherited from Vector and the List interface. They can append, insert below the top, replace by index, or resize the stack.

MethodDescriptionTutorial
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

Remove (inherited from Vector/List)

5 methods

Inherited from Vector and List. Remove by index, by value, or everything at once without popping one by one.

MethodDescriptionTutorial
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

java.util.Stack declares only five methods of its own: push, pop, peek, empty, and search. Because Stack extends Vector, it also inherits the whole Vector and List API, such as get, add, remove, size, and toArray.
This index lists 26 guides: the five LIFO methods plus the inherited Vector and List methods that are most useful on a stack.
The top is the last element of the underlying Vector, at index size() - 1. Index 0 is the bottom, so printing a stack shows it from bottom to top, for example [10, 20, 30] where 30 is the top.
For new code, Java's own documentation recommends a Deque such as ArrayDeque for LIFO stacks. Stack still works and is common in older code and textbooks, so it is worth knowing.
Read the Java Stack hub first. Then open push(), pop(), and peek() from the first table below.

Start with push()

Put your first elements on a stack and see where the top is.

push() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful