The peek()
method in Java, part of the java.util.Stack
class, is used to view the element at the top of the stack without removing it. This method is useful when you need to inspect the top element without modifying the stack.
Table of Contents
- Introduction
peek()
Method Syntax- Understanding
peek()
- Examples
- Basic Usage
- Using
peek()
in a Loop
- Real-World Use Case
- Conclusion
Introduction
The peek()
method returns the element at the top of the stack without removing it. This method is useful for reading the top element without affecting the stack's state.
peek() Method Syntax
The syntax for the peek()
method is as follows:
public E peek()
Parameters:
- This method does not take any parameters.
Returns:
- The element at the top of the stack.
Throws:
EmptyStackException
: If the stack is empty.
Understanding peek()
The peek()
method allows you to look at the top element of the stack without removing it. This is useful for scenarios where you need to read or check the top element repeatedly without modifying the stack.
Examples
Basic Usage
To demonstrate the basic usage of peek()
, we will create a Stack
object, push some elements onto the stack, and then peek at the top element.
Example
import java.util.Stack;
public class PeekExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
// Push elements onto the stack
stack.push("apple");
stack.push("banana");
stack.push("cherry");
// Peek at the top element
System.out.println("Top element: " + stack.peek());
// The stack remains unchanged
System.out.println("Stack after peek: " + stack);
}
}
Output:
Top element: cherry
Stack after peek: [apple, banana, cherry]
Using peek()
in a Loop
This example shows how to use peek()
in a loop to inspect the top element of the stack.
Example
import java.util.Stack;
public class PeekInLoopExample {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
// Push elements onto the stack
stack.push(1);
stack.push(2);
stack.push(3);
// Peek at the top element in a loop without removing it
while (!stack.empty()) {
System.out.println("Top element before pop: " + stack.peek());
stack.pop();
}
// Check if the stack is empty
System.out.println("Is the stack empty? " + stack.empty());
}
}
Output:
Top element before pop: 3
Top element before pop: 2
Top element before pop: 1
Is the stack empty? true
Real-World Use Case
Balancing Parentheses
In real-world applications, the peek()
method can be used to check the top element of the stack when verifying balanced parentheses in an expression.
Example
import java.util.Stack;
public class ParenthesesChecker {
public static void main(String[] args) {
String expression = "((a + b) * (c + d))";
if (isBalanced(expression)) {
System.out.println("The expression has balanced parentheses.");
} else {
System.out.println("The expression does not have balanced parentheses.");
}
}
public static boolean isBalanced(String expression) {
Stack<Character> stack = new Stack<>();
for (char ch : expression.toCharArray()) {
if (ch == '(') {
stack.push(ch);
} else if (ch == ')') {
if (stack.empty() || stack.peek() != '(') {
return false;
}
stack.pop();
}
}
return stack.empty();
}
}
Output:
The expression has balanced parentheses.
Conclusion
The Stack.peek()
method is used to view the element at the top of the stack without removing it. This method is particularly useful for inspecting the top element of the stack without modifying its state. By understanding and using this method, you can efficiently manage stack-based operations in your Java applications. Always handle potential EmptyStackException
when using peek()
on an empty stack.
Comments
Post a Comment
Leave Comment