A PriorityQueue is used when the objects are supposed to be processed based on the priority. It is known that a Queue follows the First-In-First-Out algorithm, but sometimes the elements of the queue are needed to be processed according to the priority, that’s when the PriorityQueue comes into play. The PriorityQueue is based on the priority heap. The elements of the priority queue are ordered according to the natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.

Declaration:
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
where E is the type of elements held in this queue
Types of PriorityQueue
- Max Priority Queue
- Min Priority Queue
Example of Default Priority Queue
Java
import java.util.*;
class PriorityQueueDemo {
public static void main(String args[])
{
PriorityQueue<Integer> pQueue
= new PriorityQueue<Integer>();
pQueue.add( 10 );
pQueue.add( 20 );
pQueue.add( 15 );
pQueue.add( 5 );
System.out.println(pQueue.peek());
System.out.println(pQueue.poll());
System.out.println(pQueue.peek());
}
}
|
In Java, Priority Queue, by default implement min Priority Queue, If we need to change the order of Priority Queue from min to max Priority Queue, then we use some methods as follows:
- Using default Comparator Collections.reverseOrder()
- Using custom Comparator
- Using lambda expression
Method 1: Using default Comparator Collections.reverseOrder()
The Collections.reverseOrder() method is used to get a reverse behavior of the default comparator. This is a by default comparator in java.util package.
Example:
Java
import java.util.*;
class PriorityQueueDemo {
public static void main(String args[])
{
PriorityQueue<Integer> pQueue
= new PriorityQueue<Integer>(
Collections.reverseOrder());
pQueue.add( 10 );
pQueue.add( 20 );
pQueue.add( 15 );
pQueue.add( 5 );
System.out.println(pQueue.peek());
System.out.println(pQueue.poll());
System.out.println(pQueue.peek());
}
}
|
Method 2: Using custom Comparator
The java.util.PriorityQueue.comparator() method shares an important function of setting and returning the comparator that can be used to order the elements in a PriorityQueue. The method returns a null value if the queue follows the natural ordering pattern of the elements.
Example:
Java
import java.util.*;
public class PriorityQueueDemo {
public static void main(String[] args)
{
PriorityQueue<Integer> pQueue
= new PriorityQueue<Integer>(
new Comparator<Integer>() {
public int compare(Integer a, Integer b)
{
if (a < b)
return 1 ;
if (a > b)
return - 1 ;
return 0 ;
}
});
pQueue.add( 10 );
pQueue.add( 15 );
pQueue.add( 20 );
pQueue.add( 5 );
System.out.println(pQueue.peek());
System.out.println(pQueue.poll());
System.out.println(pQueue.peek());
}
}
|
Method 3: Using lambda expression
Lambda expression since Java 8 has come to use, lambda function names its input parameters a and b and returns (b-a), which is basically what the int comparator class does except it returns a-b.
Example:
Java
import java.util.*;
class PriorityQueueDemo {
public static void main(String args[])
{
PriorityQueue<Integer> pQueue
= new PriorityQueue<Integer>((a, b) -> b - a);
pQueue.add( 10 );
pQueue.add( 20 );
pQueue.add( 15 );
pQueue.add( 5 );
System.out.println(pQueue.peek());
System.out.println(pQueue.poll());
System.out.println(pQueue.peek());
}
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!