Java PriorityQueue.peek()

Syntax

PriorityQueue.peek() has the following syntax.

public E peek()

Example

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


/* w  ww .  j a v a 2 s.  co m*/
import java.util.PriorityQueue;

public class Main {
   public static void main(String args[]) {

      PriorityQueue<Integer>   prq = new PriorityQueue<Integer>(); 
       
      for ( int i = 3; i < 10; i++ ){  
         prq.add(i) ; 
      }
      
      System.out.println(prq);
      
      // get the head from the queue
      Integer head = prq.peek();
      
      System.out.println("Head of the queue is: "+ head);
   }
}

The code above generates the following result.