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) {
// Priority Queue implementing min heap of Pairs
// Creating instance of PriorityQueue by passing
// Lambda expression as a constructor parameter.
PriorityQueue<Pair<Integer, String>> pq =
new PriorityQueue<>((a, b) -> a.getKey() - b.getKey());
// Adding objects of Pair<K, V> class by passing
// keys and values as parameter in Pair constructor
pq.add(new Pair<>(8, "fox"));
pq.add(new Pair<>(4, "quick"));
pq.add(new Pair<>(2, "the"));
pq.add(new Pair<>(6, "brown"));
// Printing min heap based on the priority
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) {
// Priority Queue implementing min heap of Pairs
// Creating instance of PriorityQueue by passing
// Comparator as a constructor parameter
PriorityQueue<Pair<Integer, Integer>> pq =
new PriorityQueue<>(Comparator.comparing(Pair::getKey));
// Adding objects of Pair<K, V> class by passing
// keys and values as parameter in Pair constructor
pq.add(new Pair<>(8,80));
pq.add(new Pair<>(4,70));
pq.add(new Pair<>(9,40));
pq.add(new Pair<>(2,85));
// Printing min heap based on the priority
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 implementing Max heap
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 implementing Min heap based on values of Pair<K, V>
PriorityQueue<Pair<Integer, Integer> > pq =
new PriorityQueue<>((a, b) -> a.getValue() - b.getValue());
// PriorityQueue implementing Max heap based on values of Pair<K, V>
PriorityQueue<Pair<Integer, Integer> > pq =
new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
Similar Reads
How to Iterate over the Elements of a PriorityQueue in Java? In Java, a Priority Queue is a Data structure that allows the users to store data based on their priority so that the elements with the highest priority can be accessed in constant time. In this article, we will learn how to iterate over the elements of a PriorityQueue in Java. Example Input: Priori
2 min read
Java Program to Implement PriorityQueue API A PriorityQueue is a linear data structure in which the elements are ordered according to their natural ordering or by some custom comparator provided at the queue at construction time. In PriorityQueue, the front of the queue points to the least element, and the rear points to the greatest element
4 min read
How to Copy Elements from One PriorityQueue to Another in Java? In Java, a priority queue is a data structure that allows the users to store data based on their priority. In this article, we will learn how to copy elements from one priority queue to another in Java. Example Input: PriorityQueue original ={1,2,3,4,5}Output: PriorityQueue Copy ={1,2,3,4,5}Copy ele
2 min read
Java Program to Create Set of Pairs Using HashSet Problem statement: We need to find and print unique pairs among all given pairs. Generally, if we have to find the unique numbers among all given numbers then we just put all numbers in HashSet and print them. Let us consider a sample illustration to interpret the problem statement better. Suppose w
3 min read
How to Customize the Ordering of Elements in a PriorityQueue Using a Comparator in Java? A PriorityQueue is a data structure that allows elements to be processed based on their priority. By default, it orders elements according to their natural ordering (e.g., numeric values in ascending order). However, sometimes we need a different sorting order based on specific criteria. In this art
4 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read