Stack peek() Method in Java

The java.util.Stack.peek() method in Java is used to retrieve or fetch the first element of the Stack or the element present at the top of the Stack. The element retrieved does not get deleted or removed from the Stack.

Syntax:

STACK.peek()

Parameters: The method does not take any parameters.



Return Value: The method returns the element at the top of the Stack else returns NULL if the Stack is empty.

Exception: The method throws EmptyStackException if the stack is empty.

Below programs illustrate the java.util.Stack.peek() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate peek() function
  
import java.util.*;
  
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack<String> STACK = new Stack<String>();
  
        // Use push() to add elements into the Stack
        STACK.push("Welcome");
        STACK.push("To");
        STACK.push("Geeks");
        STACK.push("For");
        STACK.push("Geeks");
  
        // Displaying the Stack
        System.out.println("Initial Stack: " + STACK);
  
        // Fetching the element at the head of the Stack
        System.out.println("The element at the top of the"
                           + " stack is: " + STACK.peek());
  
        // Displaying the Stack after the Operation
        System.out.println("Final Stack: " + STACK);
    }
}

chevron_right


Output:

Initial Stack: [Welcome, To, Geeks, For, Geeks]
The element at the top of the stack is: Geeks
Final Stack: [Welcome, To, Geeks, For, Geeks]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate peek() function
  
import java.util.*;
  
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack<Integer> STACK = new Stack<Integer>();
  
        // Use push() to add elements into the Stack
        STACK.push(10);
        STACK.push(15);
        STACK.push(30);
        STACK.push(20);
        STACK.push(5);
  
        // Displaying the Stack
        System.out.println("Initial Stack: " + STACK);
  
        // Fetching the element at the head of the Stack
        System.out.println("The element at the top of the"
                           + " stack is: " + STACK.peek());
  
        // Displaying the Stack after the Operation
        System.out.println("Final Stack: " + STACK);
    }
}

chevron_right


Output:

Initial Stack: [10, 15, 30, 20, 5]
The element at the top of the stack is: 5
Final Stack: [10, 15, 30, 20, 5]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.