Java Stack class is a part of the Java Collection Framework that represents a stack data structure based on the Last-In, First-Out (LIFO) principle. It is used to store and manage elements where the last inserted element is accessed first.
In this chapter, you will learn about the Java Stack class, its features, working, and how it is used to store and manage data using the LIFO principle.
The Stack is a class in the Collection Framework that extends the Vector class. It represents a Last-In, First-Out (LIFO) stack of objects, where the last inserted element is the first one to be removed.
It implements List, Collection, Iterable, Cloneable, and Serializable interfaces. Stack uses generics (<E>) to allow storing elements of any data type.

The Stack class provides various methods to perform operations like push, pop, peek, and search, making it useful for handling structured data efficiently.
The Stack class has only one constructor which creates an empty stack.
Here is the syntax:
If we want to create a stack, first, import the java.util package and create an object of the Stack class.
Or
Where type denotes the type of stack like Integer, String, etc.
The Stack class provides methods to perform operations such as push, pop, peek, and search on stack elements.
Stack class mainly provides five methods to perform these operations. In addition to these, it also inherits all the methods of the Vector class.
| Method | Modifier and Type | Method Description |
|---|---|---|
| empty() | boolean | The empty() method checks the stack is empty or not. |
| push(E item) | E | The push() method pushes (insert) an element onto the top of the stack. |
| pop() | E | The pop()method removes an element from the top of the stack and returns the same element as the value of that function. |
| peek() | E | The peek()method looks at the top element of the stack without removing it. |
| search(Object o) | int | The search() method searches the specified object and returns the position of the object. |
The empty() method of the Stack class check the stack is empty or not. If the stack is empty, it returns true if the stack contains no elements, otherwise it returns false. We can also use the isEmpty() method of the Vector class.p>
Syntax
It has the following syntax:
Returns: The method returns true if the stack is empty, else returns false.
In the following example, we have created an instance of the Stack class. After that, we have invoked the empty() method two times. The first time it returns true because we have not pushed any element into the stack. After that, we have pushed elements into the stack. Again we have invoked the empty() method that returns false because the stack is not empty.
Output:
Is the stack empty? true Elements in Stack: [78, 113, 90, 120] Is the stack empty? false
The method inserts an item onto the top of the stack. It works the same as the method addElement(item) method of the Vector class. It passes a parameter item to be pushed into the stack.
Syntax
It has the following syntax:
Parameter: An item to be pushed onto the top of the stack.
Returns: The method returns the argument that we have passed as a parameter.
The method removes an object at the top of the stack and returns the same object. It throws EmptyStackException if the stack is empty.
Syntax
It has the following syntax:
Returns: It returns an object that is at the top of the stack.
Let's implement the stack in a Java program and perform push and pop operations.
Output:
stack: [] push -> 20 stack: [20] push -> 13 stack: [20, 13] push -> 89 stack: [20, 13, 89] push -> 90 stack: [20, 13, 89, 90] push -> 11 stack: [20, 13, 89, 90, 11] push -> 45 stack: [20, 13, 89, 90, 11, 45] push -> 18 stack: [20, 13, 89, 90, 11, 45, 18] pop -> 18 stack: [20, 13, 89, 90, 11, 45] pop -> 45 stack: [20, 13, 89, 90, 11] pop -> 11 stack: [20, 13, 89, 90]
The peek() method returns the top element of the stack without removing it. It allows us to inspect the element at the top of the stack without modifying the stack's contents. It looks at the element that is at the top in the stack. It also throws EmptyStackException if the stack is empty.
Syntax
It has the following syntax:
Returns: It returns the top elements of the stack.
Let's see an example of the peek() method.
Output:
Stack: [Apple, Grapes, Mango, Orange] Element at the top of the stack: Orange
The method searches the object in the stack from the top. It parses a parameter that we want to search for. It returns the 1-based location of the object in the stack. These topmost object of the stack is considered at distance 1.
Suppose, o is an object in the stack that we want to search for. The method returns the distance from the top of the stack of the occurrence nearest the top of the stack. It uses equals() method to search an object in the stack.
Syntax
It has the following syntax:
Parameter: o is the desired object to be searched.
Returns: It returns the object location from the top of the stack. If it returns -1, it means that the object is not on the stack.
Let's see an example of the search() method.
Output:
Stack: [Mac Book, HP, DELL, Asus] Location of Dell: 3
Java Stack provides various operations to manipulate and access elements such as finding size and iterating through elements.
This operation is used to find the number of elements present in the stack using the size() method.
Syntax
Here is the syntax:
It has the following syntax
This example demonstrates how to check whether the stack is empty and find its size.
Output:
Is the stack empty or not? false The stack size is: 5
Iteration means accessing each element of the stack. We can iterate through the stack using different methods.
IThis method returns an iterator to traverse the elements of the stack.
Syntax
It has the following syntax
This example demonstrates how to iterate over stack elements using the iterator.
Output:
BMW Audi Ferrari Bugatti Jaguar
This method is used to iterate elements using a lambda expression.
Syntax
It has the following syntax
This example demonstrates how to iterate over stack elements using the forEach() method.
Output:
Iteration over the stack using forEach() Method: 119 203 988
This method returns a list iterator and is used to iterate the stack from top to bottom.
Syntax
It has the following syntax:
This example demonstrates how to iterate over the stack from top to bottom.
Output:
Iteration over the Stack from top to bottom: 988 203 119
We request you to subscribe our newsletter for upcoming updates.