# Java Max Priority Queue

Sheeraz Gul Mar 24, 2022

Priority Queue is a data structure in java in which elements are present according to their natural state, not according to the first in first out order. The elements can also be sorted according to a comparator used in a priority Queue.

This tutorial demonstrates the use of a priority queue and how to get maximum values from a priority queue.

## The Use of Priority Queue In Java

As mentioned above, the elements are present in a priority queue in their natural state. Let’s see an example.

Code:

``````package delftstack;
import java.util.*;

public class Priority_Queue{
public static void main(String args[]){
PriorityQueue<String> delftstack_queue=new PriorityQueue<String>();
//Add the values to the priority queue
System.out.println("Head of the PriorityQueue, The minimum value: "+delftstack_queue.element());
//All Elements of the Priority Queue
System.out.println("\nAll PriorityQueue Elements:");
Iterator demo_iterator=delftstack_queue.iterator();
while(demo_iterator.hasNext()){
System.out.print(demo_iterator.next() + " ");
}
}
}
``````

The above code will first print the head of the priority queue, which will be the minimum value, and print all the elements.

Output:

``````Head of the PriorityQueue, The minimum value: delftstack1

All PriorityQueue Elements:
delftstack1 delftstack3 delftstack2 delftstack4 delftstack5 delftstack6
``````

As we can see, the head is the minimum value. Next, we demonstrate how to get maximum values from a priority queue in Java.

## Get Maximum Values From a Priority Queue in Java

To get the maximum values from a priority queue, we should first sort them according to descending order. To sort elements in descending order, we can use a comparator to get the maximum value from the priority queue in JAVA.

Example:

``````package delftstack;
import java.util.*;

public class Priority_Queue{
public static void main(String args[]){
//Initialize a priority queue with a custom comparator to sort the queue in descending order.
PriorityQueue<Integer> demo_priority_queue = new PriorityQueue<Integer>(new Comparator<Integer>() {
public int compare(Integer left_hand_side, Integer right_hand_side) {
if (left_hand_side < right_hand_side) return +1;
if (left_hand_side.equals(right_hand_side)) return 0;
return -1;
}
});
//display the max PriorityQueue
System.out.println("The Priority Queue elements in max to min order:");
Integer val = null;
while( (val = demo_priority_queue.poll()) != null) {
System.out.print(val + " ");
}
}
}
``````

The code above sorts the priority queue in descending order to get the maximum value.

Output:

``````The Priority Queue elements in max to min order:
20 18 17 11 10 7 5 3 2
``````

Here are more ways to sort the priority queue in descending order to get the maximum value.

Example:

``````package delftstack;
import java.util.*;

public class Priority_Queue{
public static void main(String args[]){
//Initialize a priority queue with a custom comparator to sort the queue in descending order.
PriorityQueue<Integer> demo_priority_queue = new PriorityQueue<Integer>(Collections.reverseOrder());
//PriorityQueue<Integer> demo_priority_queue = new PriorityQueue<Integer>((a,b) -> b - a);
//PriorityQueue<Integer> demo_priority_queue = new PriorityQueue<Integer>((a,b) -> b.compareTo(a));
//display the max PriorityQueue
System.out.println("The Priority Queue elements in max to min order:");
Integer val = null;
while( (val = demo_priority_queue.poll()) != null) {
System.out.print(val + " ");
}
}
}
``````

`Collections.reverseOrder()` is a built-in comparator to sort the priority queue in descending order. The other two comparators in the comments also perform the same operation, and we can use any of them.

Output:

``````The Priority Queue elements in max to min order:
20 18 17 11 10 7 5 3 2
``````

The difference between the manual comparator and built-in is that we can also sort the string with built-in comparators and get the maximum value like the code snippet below.

Example:

``````package delftstack;
import java.util.*;

public class Priority_Queue{
public static void main(String args[]){
PriorityQueue<String> delftstack_queue=new PriorityQueue<String>(Collections.reverseOrder());
//Add the values to the priority queue
System.out.println("Head of the PriorityQueue, The maximum value: "+delftstack_queue.element());
//All Elements of the Priority Queue
System.out.println("\nAll PriorityQueue Elements:");
Iterator demo_iterator=delftstack_queue.iterator();
while(demo_iterator.hasNext()){
System.out.print(demo_iterator.next() + " ");
}
}
}
``````

Output:

``````Head of the PriorityQueue, The maximum value: delftstack6

All PriorityQueue Elements:
delftstack6 delftstack4 delftstack5 delftstack2 delftstack3 delftstack1
``````
Author: Sheeraz Gul

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.