The Stack peek() method in Java is an essential concept for understanding how stack data structures work. It allows developers to access the top element of a stack without removing it. This guide explains the peek method in a clear, structured, and beginner-friendly manner while also covering practical use cases and advanced considerations.
A Stack is a linear data structure that follows the Last In, First Out (LIFO) principle. The element added most recently is the first one to be removed.
In Java, the Stack class is part of the java.util package and extends the Vector class.
The peek() method in Java Stack returns the element at the top of the stack without removing it. This makes it useful when you want to inspect the current top element while keeping the stack unchanged.
E peek()
The method returns the element of type E present at the top of the stack.
Internally, the peek() method accesses the element at index size() - 1. Since Stack extends Vector, it uses indexed storage to retrieve the top element.
| Method | Purpose | Stack Modified |
|---|---|---|
| push() | Adds element to the top | Yes |
| pop() | Removes and returns top element | Yes |
| peek() | Returns top element only | No |
import java.util.Stack; public class StackPeekExample { public static void main(String[] args) { Stackstack = new Stack<>(); stack.push(5); stack.push(10); stack.push(15); System.out.println("Top element: " + stack.peek()); System.out.println("Stack after peek: " + stack); } }
Text editors use stacks to track actions. peek() helps check the latest action before undoing.
peek() is used to inspect operators while parsing arithmetic expressions.
Browsers manage page history using stacks. peek() shows the current page without altering history.
Calling peek() on an empty stack causes an EmptyStackException. Always verify the stack state.
if (!stack.isEmpty()) { System.out.println(stack.peek()); }
class Task { String name; Task(String name) { this.name = name; } } StacktaskStack = new Stack<>(); taskStack.push(new Task("Review Code")); Task currentTask = taskStack.peek();
This shows that peek() works with primitive wrappers and custom objects alike.
Undo operations in applications like text editors or design software often rely on stacks to track user actions. Every action performed by the user (typing, deleting, formatting) is pushed onto a stack. When the user clicks "Undo," the application uses the peek() method to inspect the most recent action before deciding whether to remove it or perform additional checks.
// Example of undo operation using Java Stack import java.util.Stack; public class UndoExample { public static void main(String[] args) { Stackactions = new Stack<>(); // User performs actions actions.push("Typed 'Hello'"); actions.push("Deleted 'o'"); actions.push("Typed ' World'"); // Peek the last action System.out.println("Last action: " + actions.peek()); // Outputs: Typed ' World' // Undo the last action String undoneAction = actions.pop(); System.out.println("Undone action: " + undoneAction); System.out.println("Stack after undo: " + actions); } }
Explanation:
peek() method allows inspecting the last action without removing it.pop() method is used to actually undo the action.It allows you to view the top element without removing it.
No, the stack remains unchanged.
EmptyStackException if the stack has no elements.
Both are constant-time operations, but peek() avoids modification.
Yes, it fully supports generic types.
The Stack peek() method in Java provides a safe and efficient way to access the top element of a stack without modifying it. By understanding its behavior, differences from pop(), and real-world use cases, developers can write cleaner and more reliable Java code.
Copyrights © 2024 letsupdateskills All rights reserved