The stack is a linear data structure that is used to store the collection of objects. It is based on Last-In-First-Out (LIFO). Java collection framework provides many interfaces and classes to store the collection of objects. One of them is the Stack class that provides different operations such as push, pop, search, etc.
In this section, we will discuss the Java Stack class, its methods, and implement the stack data structure in a Java program. But before moving to the Java Stack class have a quick view of how the stack works.
The stack data structure has the two most important operations that are push and pop. The push operation inserts an element into the stack and pop operation removes an element from the top of the stack. Let's see how they work on stack.
Let's push 20, 13, 89, 90, 11, 45, 18, respectively into the stack.
Let's remove (pop) 18, 45, and 11 from the stack.
Empty Stack: If the stack has no element is known as an empty stack. When the stack is empty the value of the top variable is -1.
When we push an element into the stack the top is increased by 1. In the following figure,
When we pop an element from the stack the value of top is decreased by 1. In the following figure, we have popped 9.
The following table shows the different values of the top.
Java Stack Class
In Java, Stack is a class that falls under the Collection framework that extends the Vector class. It also implements interfaces List, Collection, Iterable, Cloneable, Serializable. It represents the LIFO stack of objects. Before using the Stack class, we must import the java.util package. The stack class arranged in the Collections framework hierarchy, as shown below.
Stack Class Constructor
The Stack class contains only the default constructor that creates an empty stack.
Creating a Stack
If we want to create a stack, first, import the java.util package and create an object of the Stack class.
Where type denotes the type of stack like Integer, String, etc.
Methods of the Stack Class
We can perform push, pop, peek and search operation on the stack. The Java Stack class provides mainly five methods to perform these operations. Along with this, it also provides all the methods of the Java Vector class.
Stack Class empty() Method
The empty() method of the Stack class check the stack is empty or not. If the stack is empty, it returns true, else returns false. We can also use the isEmpty() method of the Vector class.
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.
Is the stack empty? true Elements in Stack: [78, 113, 90, 120] Is the stack empty? false
Stack Class push() Method
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.
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.
Stack Class pop() Method
The method removes an object at the top of the stack and returns the same object. It throws EmptyStackException if the stack is empty.
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.
stack:  push -> 20 stack:  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]
Stack Class peek() Method
It looks at the element that is at the top in the stack. It also throws EmptyStackException if the stack is empty.
Returns: It returns the top elements of the stack.
Let's see an example of the peek() method.
Stack: [Apple, Grapes, Mango, Orange] Element at the top of the stack: Orange
Stack Class search() Method
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. Thes 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.
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.
Java Stack Operations
Size of the Stack
We can also find the size of the stack using the size() method of the Vector class. It returns the total number of elements (size of the stack) in the stack.
Let's see an example of the size() method of the Vector class.
Is the stack empty or not? false The stack size is: 5
Iterate means to fetch the elements of the stack. We can fetch elements of the stack using three different methods are as follows:
Using the iterator() Method
It is the method of the Iterator interface. It returns an iterator over the elements in the stack. Before using the iterator() method import the java.util.Iterator package.
Let's perform an iteration over the stack.
BMW Audi Ferrari Bugatti Jaguar
Using the forEach() Method
Java provides a forEach() method to iterate over the elements. The method is defined in the Iterable and Stream interface.
Let's iterate over the stack using the forEach() method.
Iteration over the stack using forEach() Method: 119 203 988
Using listIterator() Method
This method returns a list iterator over the elements in the mentioned list (in sequence), starting at the specified position in the list. It iterates the stack from top to bottom.
Parameter: The method parses a parameter named index.
Returns: This method returns a list iterator over the elements, in sequence.
Exception: It throws IndexOutOfBoundsException if the index is out of range.
Let's iterate over the stack using the listIterator() method.
Iteration over the Stack from top to bottom: 988 203 119