Java Collection Tutorial - Java PriorityQueue.peek()


PriorityQueue.peek() has the following syntax.

public E peek()


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

//w w  w.  ja  v  a 2 s . c o 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) ; 
      // 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.