Java Program to Implement PriorityQueue API

This Java program Implements PriorityQueue API.An unbounded priority queue based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

Here is the source code of the Java Program to Implement PriorityQueue. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.PriorityQueue;
  5.  
  6. public class PriorityQueueImpl<E>
  7. {
  8.     private PriorityQueue<E> priorityQueue;
  9.  
  10.     /**
  11.      * Creates a PriorityQueue with the default initial capacity (11) that
  12.      * orders its elements according to their natural ordering.
  13.      **/
  14.     public PriorityQueueImpl()
  15.     {
  16.         priorityQueue = new PriorityQueue<E>();
  17.     }
  18.  
  19.     /**
  20.      * Creates a PriorityQueue containing the elements in the specified
  21.      * collection.
  22.      **/
  23.     public PriorityQueueImpl(Collection<? extends E> c)
  24.     {
  25.         priorityQueue = new PriorityQueue<E>(c);
  26.     }
  27.  
  28.     /**
  29.      * Creates a PriorityQueue with the specified initial capacity that orders
  30.      * its elements according to their natural ordering.
  31.      **/
  32.     public PriorityQueueImpl(int initialCapacity)
  33.     {
  34.         priorityQueue = new PriorityQueue<E>(initialCapacity);
  35.     }
  36.  
  37.     /**
  38.      * Creates a PriorityQueue with the specified initial capacity that orders
  39.      * its elements according to the specified comparator.
  40.      **/
  41.     public PriorityQueueImpl(int initialCapacity, Comparator<? super E> comparator)
  42.     {
  43.         priorityQueue = new PriorityQueue<E>(initialCapacity, comparator);
  44.     }
  45.  
  46.     /** Inserts the specified element at the tail of this queue. **/
  47.     public boolean add(E e)
  48.     {
  49.         return priorityQueue.add(e);
  50.     }
  51.  
  52.     /** Atomically removes all of the elements from this queue. **/
  53.     public void clear()
  54.     {
  55.         priorityQueue.clear();
  56.     }
  57.  
  58.     /** Returns true if this queue contains the specified element. **/
  59.     public boolean contains(Object o)
  60.     {
  61.         return priorityQueue.contains(o);
  62.     }
  63.  
  64.     /** Returns an iterator over the elements in this queue in proper sequence. **/
  65.     public Iterator<E> iterator()
  66.     {
  67.         return priorityQueue.iterator();
  68.     }
  69.  
  70.     /**
  71.      * Inserts the specified element at the tail of this queue if it is possible
  72.      * to do so immediately without exceeding the queue's capacity, returning
  73.      * true upon success and false if this queue is full.
  74.      **/
  75.     public boolean offer(E e)
  76.     {
  77.         return priorityQueue.offer(e);
  78.     }
  79.  
  80.     /**
  81.      * Retrieves, but does not remove, the head of this queue, or returns null
  82.      * if this queue is empty.
  83.      **/
  84.     public E peek()
  85.     {
  86.         return priorityQueue.peek();
  87.     }
  88.  
  89.     /**
  90.      * Retrieves and removes the head of this queue, or returns null if this
  91.      * queue is empty.
  92.      **/
  93.     public E poll()
  94.     {
  95.         return priorityQueue.poll();
  96.     }
  97.  
  98.     /**
  99.      * Removes a single instance of the specified element from this queue, if it
  100.      * is present.
  101.      **/
  102.     public boolean remove(Object o)
  103.     {
  104.         return priorityQueue.remove(o);
  105.     }
  106.  
  107.     /** Returns the number of elements in this queue. **/
  108.     public int size()
  109.     {
  110.         return priorityQueue.size();
  111.     }
  112.  
  113.     /**
  114.      * Returns an array containing all of the elements in this queue, in proper
  115.      * sequence.
  116.      **/
  117.     public Object[] toArray()
  118.     {
  119.         return priorityQueue.toArray();
  120.     }
  121.  
  122.     /**
  123.      * Returns an array containing all of the elements in this queue, in proper
  124.      * sequence; the runtime type of the returned array is that of the specified
  125.      * array.
  126.      **/
  127.     public <T> T[] toArray(T[] a) 
  128.     {
  129.         return priorityQueue.toArray(a);
  130.     }
  131.  
  132.     public static void main(String... arg)
  133.     {
  134.         PriorityQueueImpl<Integer> priorityQueue = new PriorityQueueImpl<Integer>();
  135.         priorityQueue.add(200);
  136.         priorityQueue.add(49);
  137.         priorityQueue.add(-400);
  138.         priorityQueue.add(240);
  139.         priorityQueue.add(0);
  140.         System.out.println("the elements of the priorityQueue is ");
  141.         Iterator<Integer> itr = priorityQueue.iterator();
  142.         while (itr.hasNext())
  143.         {
  144.             System.out.print(itr.next() + "\t");
  145.         }
  146.         System.out.println();
  147.         priorityQueue.offer(600);
  148.         priorityQueue.offer(700);
  149.         System.out.println("the peak element of the priorityQueue is(by peeking) " + priorityQueue.peek());
  150.         System.out.println("the peak element of the priorityQueue is(by polling) " + priorityQueue.poll());
  151.         System.out.println("element 300 removed " + priorityQueue.remove(300));
  152.         System.out.println("the priorityQueue contains 400 :" + priorityQueue.contains(400));
  153.         System.out.println("the priorityQueue contains 100 :" + priorityQueue.contains(200));
  154.         System.out.println("the size of the priorityQueue is " + priorityQueue.size());
  155.     }
  156. }

$ javac PriorityQueueImpl.java
$ java PriorityQueueImpl
the elements of the priorityQueue is 
-400	0	49	240	200	
the peak element of the priorityQueue is(by peeking) -400
the peak element of the priorityQueue is(by polling) -400
element 300 removed false
the priorityQueue contains 400 :false
the priorityQueue contains 100 :true
the size of the priorityQueue is 6

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.