Java Stack Class
In this tutorial, we will learn about java.util.Stack
, its methods and use cases of it along with a working example.
Stack Class
java.util.Stack
is a class which extends Vector
class, and it follows the order of a last-in-first-out (LIFO) manner of elements where elements can be added or removed only from the top of the Stack unlike the Queue. The Stack
class provides additional methods to perform standard stack-based operations such as push, pop, and peek.
The Stack
class is a useful implementation of the stack data structure in Java and It provides methods for adding, removing, and examining elements in the stack, and can be used in a variety of applications where a LIFO data structure is needed.
But it is important to note that the Stack
class is extends the Vector
class, which has been largely replaced by more efficient implementations such as ArrayList
and LinkedList
.
Stack class methods:
pop()
method is used to remove the top element from the stack and returns top of the element.
peek()
method is used to examine the top element without removing it and returns top of the element.
search()
method is used to find the index of an element in the current stack.
Stack use cases
The Stack class can be used in a variety of applications where a stack data structure is needed and some of the common use cases of it.
- Expression evaluation, stack-based implementation is suitable for evaluating an expression.
- Used in implementation of redo or undo operations, such as edited changes can be reverted and vice versa.
- Useful for the syntax validation, such as following proper parenthesis and braces are closed properly.
Stack class example:
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
// push elements onto the stack
stack.push("A");
stack.push("B");
stack.push("C");
// Get the top element of stack using pop method
String top = stack.pop();
System.out.println("Top of stack: " + top);
// Get the top element of stack without removing it
String peek = stack.peek();
System.out.println("Peek at top: " + peek);
// search an element in the stack
int index = stack.search("B");
System.out.println("Index of 'B': " + index);
// print the elements of the stack
System.out.println("Stack elements: " + stack);
}
}
Output:
Top of stack: C
Peek at top: B
Index of 'B': 1
Stack elements: [A, B]
Explanation:
In the above example, we create a new Stack
class object and pushing three elements onto it after use method to get the top element of Stack using pop()
method, this will return top element and removes it from the stack and used a peek()
method on stack to examine the top element and this method will not remove the head of stack.
We used search()
method to find the index of an element in the stack and finally we printed entire stack to know what elements are present in the stack using the toString
()
method.
Conclusion:
In this tutorial, we have covered what is Vector, its methods with explanation and its uses cases along with working example of it.