import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");
System.out.println(stack.pop()); // Cherry
System.out.println(stack.peek()); // Banana
System.out.println(stack.isEmpty()); // false
System.out.println(stack.search("Apple")); // 1
}
}
Java Stack: The Legacy Data Structure That Still Haunts Interviews
The Java Stack class is technically legacy. But it still shows up in interviews, so here we are. A java stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle, meaning the last element added will be the first one to be removed.
Stack Basics
A stack can be thought of as a vertical pile of plates. You can add plates to the top of the pile (push) and remove plates from the top of the pile (pop). You can also take a look at the top plate without removing it (peek).

Push, Pop, and Peek
The three main operations you can perform on a stack are:
- Push: Adds an element to the top of the stack.
- Pop: Removes the top element from the stack.
- Peek: Returns the top element without removing it.
Stack<String> stack = new Stack<>();
stack.push("Apple");
System.out.println(stack.pop()); // Apple
stack.push("Banana");
System.out.println(stack.peek()); // Banana
Stack vs Deque
Java's Stack class is actually a subclass of the Vector class, which is a synchronized implementation of a dynamic array. However, the recommended way to implement a stack in Java is to use the Deque interface, specifically the ArrayDeque class.
Deque is a double-ended queue that can be used as both a stack and a queue. It's also unsynchronized, making it faster than the legacy Stack class.
ArrayDeque: The Better Stack Alternative
If you're implementing a stack in a real-world application, you should use ArrayDeque instead of the legacy Stack class. Here's an example:
import java.util.Deque;
import java.util.ArrayDeque;
public class Main {
public static void main(String[] args) {
Deque<String> stack = new ArrayDeque<>();
stack.push("Apple");
stack.push("Banana");
System.out.println(stack.pop()); // Banana
}
}
Real-World Uses of Java Stack
So, why do we still care about the java stack? Well, it's still useful in certain scenarios:
- Undo functionality: Many text editors and word processors use a stack to implement undo functionality. Each action is pushed onto the stack, and when you want to undo, you pop the top action off the stack.
- Parsing: Stacks are useful when parsing expressions or syntax in programming languages.
- Depth-First Search (DFS): Stacks are used in DFS algorithms to traverse graphs or trees.
However, if you're not careful, you might end up with a

Performance
The legacy Stack class is synchronized, which makes it slower than the unsynchronized ArrayDeque class. However, the performance difference is usually negligible unless you're working with very large datasets.
In general, you should use the java stack that's best suited for your specific use case. If you're working on a legacy system or need to implement a stack for a specific algorithm, the legacy Stack class might be sufficient. But for most cases, ArrayDeque is the better choice.
Need help converting your legacy code to use ArrayDeque? Try CodeConverter today!