Updated June 27, 2023
Introduction to Stack Class in Java
Stack class is a part of java.util package, which implements a stack data structure. Stack class in java operates on the principle of LIFO (Last in First Out). The Stack class offers fundamental push and pop operations and additional functionalities such as empty, peek, and search. The stack class provides different methods for performing different operations.
Syntax:
Here is a basic syntax of how stack class is used in java:
import java.util.*;
// instantiate stack through default constructor
Public class Myclass{
// instantiate stack class
Stack stack = new Stack();
stack.push(1); // calling push method to insert an element
}
How does Stack Class work in java?
Stack class is a part of the java.util package, which extends the Vector class and implements the List interface. The Vector class is resizable in java, meaning it can grow in size in case of adding new elements and shrinks in size after removing elements. Since the stack class extends vector, it is also resizable in nature. To use a stack class, we have to create an instance of a stack class through a constructor. Since the stack class is resizable, only one default constructor is available in it.
After getting the instance, the following methods can be invoked as per requirement:
- push(): This method inserts an element on top of the stack and returns the inserted element.
- pop(): This method removes the last inserted element from the stack and returns the removed element.
- peek(): This method returns top elements from the stack without removing them from the stack.
- search (Object element): This method searches for a specified element in the stacks and returns its index from the top of the stack if the element is found; otherwise, it returns -1.
- empty(): This method checks if the given stack is empty. It returns true if the stack is empty; otherwise, it returns false.
- size(): This method returns a number of elements available in the stack.
Examples of Stack Class in Java
Some of the examples are given below:
Example #1
Now we will see a java code example of how the methods mentioned above are used in the stack.
Code:
//import stack class
import java.util.Stack;
public class StackDemo {
public static void main(String[] args) {
// Creating Instance of Stack
Stack<String> numbers = new Stack<>(); // stack of type string only string type elements can be inserted
// Pushing new elements to the Stack
numbers.push("One");
numbers.push("Two");
numbers.push("Three");
numbers.push("Four");
int size= numbers.size(); // finding size of stack
System.out.println("Stack contains => " + numbers);
System.out.println("Size of Stack is => " + size);
System.out.println();
// Popping Elements from the Stack
String numbersAtTop = numbers.pop(); // Throws EmptyStackException if the stack is empty
System.out.println("Element Removed => " + numbersAtTop);
System.out.println("Current State of Stack => " + numbers);
size= numbers.size();
System.out.println("Size of Stack is => " + size);
System.out.println();
// Get the element at the top of the stack without removing it
numbersAtTop = numbers.peek();
System.out.println("Top Most elemement of stack => " + numbersAtTop);
System.out.println("Current State of Stack => " + numbers);
// searching for an element in stack
int index = numbers.search("Two");
System.out.println("Element found at Index " + index);
// check if the stack is empty
boolean isempty = numbers.empty();
System.out.println("Is Stack Empty => " + isempty);
}}
The above program shows how different operations like push, pop, peek, and search, empty can be performed on stack class.
Example #2
Now we will see how it can be iterated elements of a stack class.
Code:
//import stack class
import java.util.Stack;
//import stream to iterate over stack
import java.util.stream.Stream;
public class StackDemo {
public static void main(String[] args) {
// Creating Instance of Stack
Stack<String> numbers = new Stack<>(); // stack of type string only string type elements can be inserted
// Pushing new elements to the Stack
numbers.push("First");
numbers.push("Second");
numbers.push("Third");
numbers.push("Fourth");
System.out.println("Stack contains => " + numbers);
System.out.println();
// getting stream object to iterate over elements of stack
Stream stream = numbers.stream();
System.out.println("Iterating stack using stream >> ");
stream.forEach((item) -> {
System.out.println(item); // print item
});
}
}
The above program shows how we can iterate elements of the stack. We need to call the stream() method on the stack instance, which returns the Stream object. Then we can iterate over the stack using the forEach method of the stream class.
Conclusion
In this article, we have seen about the stack class. Also, we have seen how we can instantiate the stack class and use different methods available in the stack class. We have observed various operations that one can perform using a Stack class. Java code examples shown above provide complete clarity about the stack class.
Recommended Articles
This is a guide to Stack Class in Java. Here we discuss how we can instantiate the stack class and use different methods available in the stack class, along with different examples and its code implementation. You may also look at the following articles to learn more –