Stack in Java
In Java, Stack is a class inside the java.util package and is considered a legacy collection. Even though Java provides modern alternatives like ArrayDeque, the Stack class is still widely used for learning and interviews.
What is Stack in Java?
A Stack is a LIFO (Last-In, First-Out) data structure where elements are added and removed only from the top.
It is a legacy class in Java, extends Vector, and is synchronized.
Where Stack Fits in Java Collection Hierarchy
Iterable
└── Collection
└── List
└── Vector
└── Stack
Stack also inherits:
- Synchronization
- Dynamic growing capability
- RandomAccess, Cloneable, Serializable (via Vector)
Key Features of Stack
| Feature | Explanation |
|---|---|
| LIFO Structure | Last element added is the first removed |
| Thread-Safe | Inherited synchronization from Vector |
| Allows Null | Yes |
| Allows Duplicates | Yes |
| Legacy Class | Replaced by ArrayDeque in modern Java |
| Dynamic Size | Grows like Vector |
When to Use Stack
Use Stack when:
- You need LIFO behavior
- You want built-in synchronization
- Working with older codebases
Avoid Stack when:
- You want fast, modern LIFO structure → use ArrayDeque
How to Create a Stack in Java
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("Java");
stack.push("Python");
stack.push("C++");
System.out.println(stack);
}
}
Output:
[Java, Python, C++]Common Stack Methods

1. Add / Push Methods
push(E item)
Adds an element on top of the stack.
stack.push("HTML");
2. Get / Access Methods
peek()
Returns the top element without removing it.
stack.peek();
3. Remove Methods
pop()
Removes and returns the top element.
stack.pop();
remove(int index)
Removes element at specific index (inherited from Vector).
stack.remove(0);
4. Search Methods
search(Object o)
Returns 1-based position from top; returns -1 if not found.
stack.search("Java");
contains(Object o)
Checks if the element exists.
stack.contains("Python");
5. Utility Methods
empty()
Checks if the stack is empty.
stack.empty();
size()
Returns number of elements.
stack.size();
Time Complexity of Stack
| Operation | Time Complexity | Explanation |
|---|---|---|
| push() | O(1) | Insert at top |
| pop() | O(1) | Remove top |
| peek() | O(1) | Access top |
| search() | O(n) | Linear search |
| contains() | O(n) | Linear search |
Stack vs ArrayDeque
| Feature | Stack | ArrayDeque |
|---|---|---|
| Legacy | Yes | No |
| Synchronized | Yes | No |
| Speed | Slower | Faster |
| Recommended | No | Yes |
| Implementation | Vector | Resizable array |
Advantages of Stack
- Simple LIFO operations
- Thread-safe (via Vector)
- Easy to use in multithreaded context
Disadvantages of Stack
- Slow due to synchronization
- Considered outdated
- ArrayDeque is faster and modern
- Inherits overhead from Vector
Real-World Use Cases of Stack
- Expression evaluation (postfix/prefix)
- Undo/Redo operations
- Parsing (XML, HTML)
- DFS (Depth-First Search)
- Backtracking algorithms
- Browser navigation (Back button)
