Skip to content

Java Stack

What exactly is a Java Stack, you ask?

Well, think of it as a stack of plates in a cafeteria—last in, first out. You can push items onto the top of the stack and pop them off when you need them. It's a simple yet powerful way to manage data in your Java programs.

Imagine you're at a breakfast buffet, and you're stacking up your plate with delicious pancakes. Each time you grab a pancake, you add it to the top of your stack. When it's time to eat, you start with the pancake on the top of the stack—the last one you added—and work your way down.

In Java, you can create a Stack object and use its methods to push items onto the stack and pop them off when needed. Let's see it in action with a simple example:

java
import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();

        // Pushing items onto the stack
        stack.push("Pancake 1");
        stack.push("Pancake 2");
        stack.push("Pancake 3");

        // Popping items off the stack
        while (!stack.isEmpty()) {
            System.out.println("Eating " + stack.pop());
        }
    }
}

In this example, we create a Stack object to hold our pancake plates. We push three pancakes onto the stack and then start eating them one by one by popping them off. Just like at the buffet, we start with the last pancake we added and work our way down.

Peeking at the top item without removing it

The peek() method allows us to look at the top item on the stack without removing it. This is useful for inspecting the next item in line without consuming it.

java
import java.util.Stack;

public class StackExample {
 public static void main(String[] args) {
     Stack<String> stack = new Stack<>();

     // Pushing items onto the stack
     stack.push("Pancake 1");
     stack.push("Pancake 2");
     stack.push("Pancake 3");

     String topItem = stack.peek();
     System.out.println("Peeking at the top item: " + topItem);
 }
}

Searching for specific items

The search() method helps us find a specific item in the stack and returns its position (index) if found. If the item is not found, it returns -1.

java
import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();

        // Pushing items onto the stack
        stack.push("Pancake 1");
        stack.push("Pancake 2");
        stack.push("Pancake 3");

        String searchItem = "Pancake 2";
        int position = stack.search(searchItem);
        if (position != -1) {
            System.out.println(searchItem + " found at position " + position);
        } else {
            System.out.println(searchItem + " not found in the stack");
        }
    }
}

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.