Priority Queue of Pair in Java with Examples
Last Updated :
08 Jul, 2022
Prerequisite: PriorityQueue, Pair
javafx.util.Pair<K,V> class in Java is the combination of two different objects called Key and Value. It basically provides a way to store pair of two heterogeneous data items.
Pair <K, V> pair = new Pair<>(K key, V value)
PriorityQueue<E> is like a normal Queue in which the elements are ordered according to the natural ordering and comparator or lambda expression passed in the constructor.
Mostly used Constructors of Priority Queue
- PriorityQueue<E> pq = new PriorityQueue<E>();
- PriorityQueue<E> pq = new PriorityQueue<E>(Collection<E> c);
- PriorityQueue<E> pq = new PriorityQueue<E>(int initialCapacity);
- PriorityQueue<E> pq = new PriorityQueue(int initialCapacity, Comparator<E> comparator);
PriorityQueue of Pair<K, V> Syntax
- PriorityQueue<Pair<K, V>> = new PriorityQueue<>(initialCapacity, Comparator.comparing(Pair :: getKey))
- PriorityQueue<Pair<K, V>> = new PriorityQueue<>(Comparator.comparing(Pair :: getKey))
Note:
- Since Pair<K, V> class was the part of JavaFX and JavaFX was removed from JDK since JDK 11. So Pairs can be used till JDK 10.
- The below source code might not run on most of the online IDEs, better to try it on offline software.
PriorityQueue Implementing Min Heap Based on Keys of Pair
1. Using Lambda Expression:
Java
import java.util.*;
import java.io.*;
import javafx.util.Pair;
class GFG {
public static void main(String[] args) {
PriorityQueue<Pair<Integer, String>> pq =
new PriorityQueue<>((a, b) -> a.getKey() - b.getKey());
pq.add( new Pair<>( 8 , "fox" ));
pq.add( new Pair<>( 4 , "quick" ));
pq.add( new Pair<>( 2 , "the" ));
pq.add( new Pair<>( 6 , "brown" ));
while (!pq.isEmpty()){
System.out.print(pq.remove() + " " );
}
}
}
|
Output:
2=the 4=quick 6=brown 8=fox
2. Using Comparator : Comparator.comparing()
Java
import java.io.*;
import java.util.*;
import javafx.util.Pair;
class GFG {
public static void main(String[] args) {
PriorityQueue<Pair<Integer, Integer>> pq =
new PriorityQueue<>(Comparator.comparing(Pair::getKey));
pq.add( new Pair<>( 8 , 80 ));
pq.add( new Pair<>( 4 , 70 ));
pq.add( new Pair<>( 9 , 40 ));
pq.add( new Pair<>( 2 , 85 ));
while (!pq.isEmpty()){
System.out.print(pq.remove() + " " );
}
}
}
|
Output:
2=85 4=70 8=80 9=40
PriorityQueue Implementing Max Heap Based on Keys of Pair
Java by default creates PriorityQueue of min heap when we do not specify the type. A little change in the above code can make it behave like a Max heap.
Java
PriorityQueue<Pair<Integer, Integer> > pq =
new PriorityQueue<>((a, b) -> b.getKey() - a.getKey());
|
Another way to create a max heap:
- By adding the keys into PriorityQueue after multiplying them with -1.
- But do not forget to convert that into the original format (again multiplying by -1) while removing from the queue and performing operations on them.
PriorityQueue Implementing Max Heap and Min Heap Based on Values of Pair
In order to create PriorityQueue based on values of Pair<K, V> just replace getKey() method with getValue() method.
Java
PriorityQueue<Pair<Integer, Integer> > pq =
new PriorityQueue<>((a, b) -> a.getValue() - b.getValue());
PriorityQueue<Pair<Integer, Integer> > pq =
new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...