java.util.PriorityQueue.poll() Method



Description

The poll() method is used to retrieve and remove the head of this queue, or returns null if this queue is empty.

Declaration

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

public E poll()							 

Parameters

NA

Return Value

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

Exception

NA

Example

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

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.poll();

      System.out.println("Head of the queue is: "+ head);
      System.out.println("Priority queue values after poll: "+ prq);
   }
}

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
Priority queue values after poll: [4, 6, 5, 9, 7, 8]
java_util_priorityqueue.htm
Advertisements