Home >Java >javaTutorial >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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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 }
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:
Some of the examples are given below:
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.
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.
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.
The above is the detailed content of Stack Class in Java. For more information, please follow other related articles on the PHP Chinese website!