Wednesday, July 3, 2024
HomeLanguagesJavaPriorityQueue peek() Method in Java

PriorityQueue peek() Method in Java

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

Syntax:

Priority_Queue.peek()

Parameters: The method does not take any parameters.

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

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




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


Output:

Initial PriorityQueue: [For, Geeks, To, Welcome, Geeks]
The element at the head of the queue is: For
Final PriorityQueue: [For, Geeks, To, Welcome, Geeks]

Program 2:




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


Output:

Initial PriorityQueue: [5, 10, 30, 20, 15]
The element at the head of the queue is: 5
Final PriorityQueue: [5, 10, 30, 20, 15]

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments