Java Collection Tutorial - Java Stack.peek()








Syntax

Stack.peek() has the following syntax.

public E peek()

Example

In the following code shows how to use Stack.peek() method.

//from w  w w . j a va 2s  .  c o  m
import java.util.Stack;

public class Main {
   public static void main(String args[]) {
      
      Stack st = new Stack();
      
      st.push("Java");
      st.push("Source");
      st.push("from java2s.com");
      
      // checking the top object
      System.out.println("Top object is: "+st.peek());
   }     
}

The code above generates the following result.