Java Stack Class
Java Stack Class is a part of the Java Collections Framework. This class represents a last-in-first-out (LIFO) stack of objects. It extends the Vector class and uses the methods in Vector to achieve a suitable stack with traditional operations like push, pop, and peek.
What is a Stack?
A Stack is a data structure where you add elements to the "top" of the stack, and also remove elements from the top again. It's a LIFO structure, meaning Last In First Out. The last element that was added to the stack is the first one to come off.
Java Stack Class
The Stack class in Java is a part of the java.util
package. It implements a stack data structure and extends the Vector class which is a legacy class in Java. The Stack class provides five methods, namely, push()
, pop()
, peek()
, empty()
, and search()
.
Here's a brief overview of these methods:
push(Object element)
: This method is used to push an element into the Stack.pop()
: This method is used to remove and return the top element of the Stack.peek()
: This method is used to look at the top element of the Stack without removing it.empty()
: This method returns true if the Stack is empty; false otherwise.search(Object element)
: This method returns the position of an element in the Stack.
Let's look at some examples of these methods in action.
Creating a Stack
Creating a stack in Java is simple. Here's how you do it:
Stack<String> stack = new Stack<>();
This code snippet creates a new stack of Strings.
Using push()
The push()
method is used to add elements to the top of the stack. Here's an example:
stack.push("Java");
stack.push("Kotlin");
stack.push("Python");
After these lines are executed, the stack looks like this: Java -> Kotlin -> Python
.
Using pop()
The pop()
method is used to remove and return the top element of the stack. If we run stack.pop()
on our current stack, it will remove "Python" and return it.
String top = stack.pop(); // top contains "Python"
After this line is executed, the stack looks like this: Java -> Kotlin
.
Using peek()
The peek()
method is used to look at the top element of the stack without removing it. If we run stack.peek()
on our current stack, it will return "Kotlin".
String top = stack.peek(); // top contains "Kotlin"
After this line is executed, the stack remains the same: Java -> Kotlin
.
Using empty()
The empty()
method is used to check if the stack is empty. It returns true if the stack is empty, and false if it contains elements.
boolean isEmpty = stack.empty(); // isEmpty is false
Using search()
The search()
method is used to get the 1-based position of an element in the stack. If the element is found, it returns the position of the element from the top of the stack. Otherwise, it returns -1.
int position = stack.search("Java"); // position is 2
In this tutorial, we learned about the Java Stack class, its methods, and how to use them. This class is a very useful tool to have in your Java toolkit, especially when you're dealing with problems that require LIFO type of behavior.