Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – PriorityQueue

Declaration

class PriorityQueue<E>

The PriorityQueue class extends AbstractQueue class & implements the Queue interface.

It does not order the elements in FIFO manner.

List of PriorityQueue Constructors (CLICK HERE)

List of PriorityQueue Methods (CLICK HERE)

Example

 import java.util.Iterator;
 import java.util.PriorityQueue;

 public class PriorityQueueNonGen 
 {
     public static void main(String args[])
     {
         PriorityQueue<String> pq = new PriorityQueue<String>();
         pq.add("FootBall");
         pq.add("BasketBall");
         pq.add("Cricket");
         pq.add("Golf");

         Iterator<String> itr = pq.iterator();
		
         while (itr.hasNext())
         {
             System.out.println(itr.next());
         }
         System.out.println("\nBasketBall available :: "+pq.contains("BasketBall"));
         System.out.println("Hockey available :: "+pq.contains("Hockey"));
		
         System.out.println("\nElement at PEEK :: "+pq.peek());
		
         pq.poll();
		
         System.out.println("\nAfter removing element using poll function");
         itr = pq.iterator();
		
         while (itr.hasNext())
         {
             System.out.println(itr.next());
         }	
     }
 }

Output

BasketBall
FootBall
Cricket
Golf

BasketBall available :: true
Hockey available :: false

Element at PEEK :: BasketBall

After removing element using poll function
Cricket
FootBall
Golf

 

Leave a reply