java.util.PriorityQueue.peek() Method

Advertisements


Description

The peek() method is used to retrieve the head of this queue.But it does not remove it.

Declaration

Following is the declaration for java.util.PriorityQueue.peek() method.

public E peek()							 

Parameters

  • NA

Return Value

  • The method call returns the head of this queue, or null if this queue is empty.

Exception

  • NA

Example

The following example shows the usage of java.util.PriorityQueue.peek()

package com.tutorialspoint;

import java.util.*;

public class PriorityQueueDemo {
   public static void main(String args[]) {
      // create priority queue
      PriorityQueue < Integer >  prq = new PriorityQueue < Integer > (); 
       
      // insert values in the queue
      for ( int i = 3; i  <  10; i++ ){  
         prq.add (new Integer (i)) ; 
      }
      
      System.out.println ( "Initial priority queue values are: "+ prq);
      
      // get the head from the queue
      Integer head = prq.peek();
      
      System.out.println ( "Head of the queue is: "+ head);
   }
}

Let us compile and run the above program, this will produce the following result.

Initial priority queue values are: [3, 4, 5, 6, 7, 8, 9]
Head of the queue is: 3



Advertisements
Advertisements