Java PriorityQueue Class Example

In the PriorityQueue, elements are ordered in accordance to their natural ordering or the order defined by the Comparator. Comparator is the factor which compares the elements for the ordering purpose. These are defined at the queue construction time which decides the ordering of elements in the queue, based on the type of constructor used. In natural ordering, elements are ordered and retrieved on the basis of “First in First out (FIFO)”, where as in PriorityQueue elements are retrieved on their priority basis. Each element of the priority queue is assigned with a priority and thus the element with highest priority is always executed first.

also read:

The size of PriorityQueue is unbounded, but it has the internal capacity to restrict its size according to the elements in the queue. The size goes on increasing as we go on adding the elements to the queue.

PriorityQueue Class Declaration

public class PriorityQueue <E> extends AbstractQueue <E> implements Serializable

Where, E is the type of the element in the collection.

PriorityQueue Class Constructors

PriorityQueue ()It constructs a default PriorityQueue with initial capacity of 11. Elements are ordered in their natural ordering.
PriorityQueue (Collection c)It constructs a PriorityQueue for the elements given in the collection.
PriorityQueue (int initialCapacity)PriorityQueue is constructed with the given capacity and ordering of elements is done in accordance with their natural ordering.
PriorityQueue (int initialCapacity, Comparator Comparator)PriorityQueue is constructed with the given capacity which orders the elements according to the comparator.
PriorityQueue (PriorityQueue c)It constructs a PriorityQueue for the elements given in the PriorityQueue.
PriorityQueue (SortedSet c)It constructs a PriorityQueue for the elements given in the SortedSet.

PriorityQueue Class Methods

boolean add(E e)Adds the specified element to the priority queue.
void clear ()It clears the elements from the queue.
Comparator comparatorOrdering of elements in the queue is done after comparing the elements. The comparator output is null value if ordering is done in accordance to their natural ordering.
boolean contains (Object o)Make a check for the existence of the defined object in the queue. If object exists in queue, it returns boolean true value or else returns false value.
Iterator iterator()This performs the iteration over the queue but does not guarantee the particular order of elements in the queue. To get the sorted order you need to call explicitly Arrays. sort(pq. toArray ()).
boolean offer (E e)It adds newly defined value to the existing queue.
E pollAlong with indicating the head of the queue, it also removes the head of queue from the queue.
E peekIt point to the head of the queue, but does not remove it unlike the poll method does.
boolean remove (Object o)It removes the defined object value from the queue.
int size ()It returns the number of elements in the queue.
Object [] toArray ()It transforms the natural ordered queue to the ordered array queue.
T [] toArray (T [] a)It returns the elements of the priority queue in the array type. Array contains the sorted elements.

PriorityQueue Class Example

import java.util.Comparator;
import java.util.PriorityQueue;
public class Queue_example {
	public static void main(String[] args) {

		      // create priority queue
  PriorityQueue < Integer >  p1 = new PriorityQueue < Integer > ();

		      // insert values in the queue

 System.out.println ( "Priority queue values are: "+ p1);
 System.out.println ( "Size of the Priority queue is: "+ p1.size());

		      // create comparator
		      Comparator<? super Integer> c1 = p1.comparator();

		      System.out.println ( "Comparator value is: "+ c1);
		      Integer head=p1.poll();
		      System.out.println ( "Poll of the Priority queue is: "+ head);
System.out.println ( "queue after Poll is applied on Priority queue is: "+ p1);
		      Integer head1=p1.peek();
System.out.println ( "peek of the Priority queue is: "+ head1);
System.out.println ( "Priority queue after using offer is: "+ p1);
		      boolean b=p1.contains(2);
System.out.println ( "checks if 2 is present in : "+ b);
		      Object[] o1 = p1.toArray();

		      System.out.println (“Value in array: ");

		      for ( int i = 0; i<o1.length; i++ ){
System.out.println (“array content: " + o1[i].toString ()) ;
  • Above example illustrates the usage of methods add, poll, peek, size, toArray and comparator.
  • import java.util.Comparator; this package includes the comparator functions. Hence, necessary to import when using comparator method.
  • PriorityQueue p1 = new PriorityQueue (); line creates the PriorityQueue instance p1 by using the new operator.
  • p1.add (0); line adds the specified element to the PriorityQueue p1.
  • System.out.println (“Size of the Priority queue is: “+ p1.size ()); statement returns the size of the PriorityQueue p1 and displays the result on the output screen.
  • Comparator c1 = p1.comparator(); line creates the instance c1 for comparator.
  • Integer head=p1.poll (); line points to the poll value of PriorityQueue p1 and stores the poll element into variable head.
  • Integer head1=p1.peek (); line points to the peek value of PriorityQueue p1 and stores the peek element into variable head.
  • p1.offer (50); line adds the new specified element into the PriorityQueue p1.
  • boolean b=p1.contains(2); line checks whether specified element 2 is present in the PriorityQueue p1 or not and returns boolean true or false depending on the existence of element in p1. It returns true if value is present in p1, else returns boolean false value.
  • Object[] o1 = p1.toArray(); line transforms all the PriorityQueue elements into the sorted array type.

Java PriorityQueue Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!