Java Stack: Push, Pop, Peek (And Why You Should Use ArrayDeque)

February 11, 2026
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
    }
}
Cherry Banana false 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).

Stack overflow joke

Push, Pop, and Peek

The three main operations you can perform on a stack are:

Stack<String> stack = new Stack<>();
stack.push("Apple");
System.out.println(stack.pop()); // Apple
stack.push("Banana");
System.out.println(stack.peek()); // Banana
Apple 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
    }
}
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:

However, if you're not careful, you might end up with a

LIFO explanation meme
.

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!

Related Articles