Stack is a data structure that follows the LIFO (Last In First Out) order. That means, if any element is inserted in a stack, it will be inserted at the “top” position.
In this post, we’ll talk about Stack class in Java and how it works.
In Java, the stack is a legacy class that follows the stack data structure to store elements and provide a different method to perform operations.
How to Create a Stack?
The below example creates an empty Stack. Remember to import java.util.Stack;
Stack<E> stack = new Stack();
Here E is the type of Object. It can be Integer, String, etc (Non Primitives Only).
Java Stack Declaration:
Below is the exact Stack class present in the JDK:
package java.util;
public class Stack<E> extends Vector<E> {
private static final long serialVersionUID = 1224463164541339165L;
public Stack() {
}
public E push(E var1) {
this.addElement(var1);
return var1;
}
public synchronized E pop() {
int var2 = this.size();
Object var1 = this.peek();
this.removeElementAt(var2 - 1);
return var1;
}
public synchronized E peek() {
int var1 = this.size();
if (var1 == 0) {
throw new EmptyStackException();
} else {
return this.elementAt(var1 - 1);
}
}
public boolean empty() {
return this.size() == 0;
}
public synchronized int search(Object var1) {
int var2 = this.lastIndexOf(var1);
return var2 >= 0 ? this.size() - var2 : -1;
}
}
As you can see, it supports only 5 operations i.e push(), pop(), peek(), empty(), and search(). Also, it extends the Vector class therefore, it inherits all methods of the Vector class as well.
Java Stack Methods:
Below is a quick definition of Java Stack methods:
E push(E item): | Pushes an item onto the top of this stack. |
E pop(): | Removes the object from the top of the stack and returns the same value. |
E peek(): | Returns the value from the top of the stack (without removing). |
boolean empty(): | Check if the stack is empty. |
int search(Object o): | Returns the position where an object is present on the stack. |
How to Perform Operations In Stack?
Let’s see some of the famous operation that can be performed on Stacks in Java.
1. Push Operation:
import java.util.Stack;
public class StackDemo {
public static void main(String[] args) {
// Initialization of Stack (Integer Type)
Stack<Integer> stackObj = new Stack<>();
// pushing element into stack
stackObj.push(10);
stackObj.push(8);
stackObj.push(3);
// Printing the Stack Elements
System.out.println(stackObj);
}
}
Output:
[10, 8, 3]
2. Pop Operation:
public class StackDemo {
public static void main(String[] args) {
// Initialization of Stack (Integer Type)
Stack<Integer> stackObj = new Stack<>();
// pushing element into stack
stackObj.push(10);
stackObj.push(8);
stackObj.push(6);
// Printing the Stack Elements
System.out.println("Before Removing -> " + stackObj);
// Removing elements by Pop Operation
stackObj.pop();
// Printing the Stack Elements
System.out.println("After Removing -> " + stackObj);
}
}
Output:
Before Removing -> [10, 8, 6]
After Removing -> [10, 8]
As you can see, element added in the last was removed from the Stack (LIFO property)
3. Peek Operation:
public class StackDemo {
public static void main(String[] args) {
// Initialization of Stack (Integer Type)
Stack<Integer> stackObj = new Stack<>();
// pushing element into stack
stackObj.push(10);
stackObj.push(8);
stackObj.push(6);
// Getting Top Operation
Integer peekElement = stackObj.peek();
// Printing the Top Elements
System.out.println("Element at top -> " + peekElement);
}
}
Output:
Element at top -> 6
NOTE: Stack class in Java is a thread-safe class and hence involves overhead (acquiring and releasing thread-locks is relatively expensive). In a single-threaded environment, it is recommended to use ArrayDeque for stack implementation as it is more efficient.
Official Docs: Click here