The Java Stack peek() method is used to look at the object at the top of this stack without removing it from the stack.
DeclarationFollowing is the declaration for java.util.Stack.peek() method.
public Object peek()Parameters
NA
Return ValueThe method call returns the object at the top of this stack.
ExceptionEmptyStackException − This is thrown if this stack is empty.
Get Integer from the Top of Stack ExampleThe following example shows the usage of Java Stack peek() method to get the element from the top of the stack without removing it from the stack. In this example, we've created a Stack object of Integers. Then we've added few integers to the stack and printed the top element using peek() method.
package com.tutorialspoint; import java.util.Stack; public class StackDemo { public static void main(String args[]) { // creating stack Stack<Integer> st = new Stack<>(); // populating stack st.push(10); st.push(20); st.push(30); // checking the top object System.out.println("Top object is: "+st.peek()); } }Output
Let us compile and run the above program, this will produce the following result.
Top object is: 30Get String from the Top of Stack Example
The following example shows the usage of Java Stack peek() method to get the String from the top of the stack without removing it from the stack. In this example, we've created a Stack object of Strings. Then we've added few strings to the stack and printed the top element using peek() method.
package com.tutorialspoint; import java.util.Stack; public class StackDemo { public static void main(String args[]) { // creating stack Stack<String> st = new Stack<>(); // populating stack st.push("Java"); st.push("Source"); st.push("code"); // checking the top object System.out.println("Top object is: "+st.peek()); } }Output
Let us compile and run the above program, this will produce the following result.
Top object is: codeGet Object from the Top of Stack Example
The following example shows the usage of Java Stack peek() method to get the student object from the top of the stack without removing it from the stack. In this example, we've created a Stack object of Student objects. Then we've added few student objects to the stack and printed the top element using peek() method.
package com.tutorialspoint; import java.util.Stack; public class StackDemo { public static void main(String args[]) { // creating stack Stack<Student> st = new Stack<>(); // populating stack st.push(new Student(1, "Julie")); st.push(new Student(2, "Robert")); st.push(new Student(3, "Adam")); // checking the top object System.out.println("Top object is: "+st.peek()); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }Output
Let us compile and run the above program, this will produce the following result.
Top object is: [ 3, Adam ]
java_util_stack.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4