All you need to know about java stack and methods in java with examples. Basically, Stack is a data structure to store data in a particular fashion and which has its own way of adding new elements and removing element. It follows the LIFO (Last In First Out) mechanism which means the element to add to the stack most recently will be removed first at the time of deleting an element.
Java has a class named stack which has all the methods of stack data structure. In a stack, adding an element is referred to as pushing an element. Removing an element is referred to as popping out an element from the stack. So, the methods name are push() and pop() for adding and deleting an element respectively.
Table of Contents
Stack in java
In java, the Stack class is the sub-class of the Vector class. Methods present in the class are push(), pop(), empty(), search() and peek(). These are all the main methods for which the concept of the stack is used. Let us go through all the methods in the java stack class.
Methods in java stack
Push():
To add element to the stack, the push method is used. The new element added to the stack is always added at the top of the stack. We have to pass the object element as a parameter while using the push method, which will push the element to the stack.
Pop():
To remove element from the stack, the pop method is used. This method removes the element present in the top of the stack. Using it into code will return the top element of the stack and will also delete that element.
Empty():
When we declare a stack by creating an object of java stack class, it creates an empty stack. So, at any point of time if we need to check whether the stack is empty or contain any elements, we use this empty() method. A stack can also be empty if all the elements are popped out of the stack. The empty method returns a Boolean value that is if the stack is empty, then it will return true else it will return False.
Search():
As it is clear from the name that the Search() method is used to search a particular element into the stack, whether it is present or not in the stack. Here also you need to pass the object element as the parameter to the Search method(), which you need to find in the stack.
Search method will return offset of the element you need to find into the stack. Offset here is referred to as the position of the element in the stack from the top of the stack. If the element is not present in the stack, then it will return -1.
Peek():
For knowing the top element of the stack, peek() method is used. It is similar to the pop method that will return the top element but it will not remove the element from the stack.
These were the main methods that define the concept of Stack in java. These concepts are very useful. It is used where you need to access the most recent command given or we can say access the most recent element easily.
Java Stack Implementation
Let us know more about Java stack implementation with its syntax and examples.
Syntax:
Stack<DataType> var = new Stack<DataType>();
Here we replace the “DataType” with the type of data we need to store inside the stack. This will create an empty stack that will be capable of storing the type of data mentioned in place of Datatype. So, var is the object of the stack java class and with that object, we can use all the methods of stack class that we discussed above.
All these concepts would be clearer with the help of examples:
Java stack Example 1
import java.io.*;
import java.util.*;
public class Demo
{
public static void main (String[] args)
{
Stack<Integer> stack = new Stack<Integer>();
System.out.println("Program to understand how push and pop methods work");
stack.push(1);
System.out.println("1st element pushed");
stack.push(2);
System.out.println("2nd element pushed");
Integer a = (Integer) stack.pop();
System.out.println("Element popped from top: " + a);
Integer b = (Integer) stack.pop();
System.out.println("Element popped from top: " + b);
}
}
Output:
Program to understand how push and pop methods work
1st element pushed
2nd element pushed
Element popped from top: 2
Element popped from top: 1
Here in the above example, we can see that an object of stack java class is declared as “stack” which will create an empty stack. After that, we have push() method with a parameter that contains the element that needs to be pushed. Since it is an integer stack, so we passed an integer value.
First, value added is 1, so the top value in the stack is 1, then we pushed the second value 2. Now, 2 is the value on the top of the stack. After that, we popped the value from the stack. So as we know pop method will return the top element in the stack and will also delete that element from the stack. So we have 2 on the top of the stack. 2 is added lastly in the stack, so 2 will be deleted from the stack. And repeating the step again, 1 will also be removed and the final stack will be empty.
Java stack Example 2
//Example to know the working of methods of java stack class
import java.util.*;
public class Demo_class{
public static void main (String[] args)
{
// Creating a new empty stack named even
Stack<Integer> even = new Stack<Integer>();
// pushing random values in stack
even.push(4);
even.push(6);
even.push(8);
even.push(10);
even.push(12);
even.push(14);
even.push(16);
//printing the stack before pop
System.out.println(even);
// removing top element
System.out.println("pop top element:" + even.pop());
System.out.println("pop top element:" + even.pop());
System.out.println("pop top element:" + even.pop());
//printing the stack after pop operation
System.out.println(even);
// just accessing the element at the top of the stack without removing it
System.out.println("top element of stack: " + even.peek());
// checking if the stack is empty or contain any elements
System.out.println("stack empty? Ans:" + even.empty());
// checking the position of an element in the stack
System.out.println("Position of 8 in the stack " + even.search(8));
// checking for the element which is not present in the stack
System.out.println("Position of 20 in the stack " + even.search(20));
}
}
Output:
[4, 6, 8, 10, 12, 14, 16]
pop top element:16
pop top element:14
pop top element:12
[4, 6, 8, 10]
top element of stack: 10
stack empty? Ans:false
Position of 8 in the stack 2
Position of 20 in the stack -1
In the following java stack example, we used every method of the java stack class. First, we pushed/added some even digits to the stack with the push method and then removed/popped 3 elements. The elements removed were from the top. When we used the pop method, we came to know about the top element in the stack which was 10. Then next, we used the empty method to check whether the stack is empty or not since it returned a Boolean value, false was displayed.
By using the search method for searching 8 in the stack, it was at the second position from the top in the stack. And when we called the same method for 20, it returned -1 because it is not present in the stack.