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). Stack class provides basic push and pop operations in addition to other operations like empty, peek and search. Stack class provides different methods for performing different operations.
Syntax:
Here is a basic syntax of how to 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 java.util package which extends Vector class and implements List interface. Vector class is resizable in java that means it can grow in size in case of addition of new elements and shrinks in size after removal of elements. Since the stack class extends vector, it is also resizable in nature. In order to use a stack class, we have to create an instance of stack class through constructor. Since the stack class is resizable, there is only one default constructor 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 it from the stack.
- search (Object element): This method searches for a specified element into the stacks and returns its index from 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 above-mentioned methods 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);
}}
4.8 (7,954 ratings)
View Course
In the above program, we have shown how different operations like push, pop, peek, and search, empty can be performed on stack class. The above program will produce the following as the output:
Example #2
Now we will see how 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 stack instance which returns the Stream object. Then we can iterate over stack using forEach method of stream class. The above code will produce the following output:
Conclusion
In this article, we have learned 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 seen different operations that can be performed 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 –