What does the method peek() do in java?

Java 8Object Oriented ProgrammingProgramming

The peek() method is used to look at the object at the top of this stack without removing it from the stack.

Example

import java.util.*;
public class StackDemo {
   public static void main(String args[]) {
     
      Stack st = new Stack();
      st.push("Java");
      st.push("Source");
      st.push("code");
      System.out.println("Top object is: "+st.peek());
   }
}

Output

Top object is: code
raja
Published on 08-Feb-2018 08:53:30
Advertisements