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 article, we will learn to Customize the Ordering of Elements in a PriorityQueue Using a Comparator in Java.
Prerequisites:
Customize the Ordering of Elements in a PriorityQueue
Using a Custom Comparator to customize the ordering of elements in a PriorityQueue, follow these steps:
- When constructing a PriorityQueue, provide a custom comparator a define the desired order.
- The constructor PriorityQueue (int capacity, Comparator<E> comparator) allows you to create a PriorityQueue with a specified initial capacity and a custom comparator.
- The comparator determines how elements are compared and ordered.
Program to Customize the Ordering of Elements in a PriorityQueue
Let’s create a PriorityQueue of students based on their CGPA (grade point average). We’ll define a custom comparator (StudentComparator) that compares students’ CGPA values
Below is the implementation of Customize the Ordering of Elements in a PriorityQueue:
Java
// Java Program to Customize the Ordering // of Elements in a PriorityQueue import java.util.*; // Class representing a priority queue of students public class GFG { // Main method public static void main(String[] args) { // Create a scanner object for input Scanner in = new Scanner(System.in); // Create a priority queue of students with custom comparator PriorityQueue<Student> pq = new PriorityQueue<>( 5 , new StudentComparator()); // Create student objects and add them to the priority queue Student student1 = new Student( "Nandini" , 3.2 ); pq.add(student1); Student student2 = new Student( "Anmol" , 3.6 ); pq.add(student2); Student student3 = new Student( "Palak" , 4.0 ); pq.add(student3); // Print the students served in their priority order System.out.println( "Students served in their priority order:" ); while (!pq.isEmpty()) { System.out.println(pq.poll().getName()); } } } // Comparator class for comparing students based on their CGPA class StudentComparator implements Comparator<Student> { // Compare method to compare two students public int compare(Student s1, Student s2) { if (s1.cgpa < s2.cgpa) return 1 ; else if (s1.cgpa > s2.cgpa) return - 1 ; return 0 ; } } // Class representing a student class Student { // Member variables public String name; public double cgpa; // Constructor public Student(String name, double cgpa) { this .name = name; this .cgpa = cgpa; } // Method to get the name of the student public String getName() { return name; } } |
Students served in their priority order: Palak Anmol Nandini
Explanation of the above Program:
- In the example above, we create a priorityQueue of Students.
- The custom comparator (StudentComparator) orders students based on their CGPA.
- Students with higher CGPA are served first, demonstrating customized ordering.
Let’s explore another approach for customizing the ordering of elements in a PriorityQueue using a Comparator in java.
Ordering Strings by Length
Suppose we want to create a PriorityQueue that orders strings based on their length (number of characters). we’ll define a custom comparator to achieve this.Create a new Java file (e.g., StringLengthPriorityQueue.java)
Below is the implementation of Ordering Strings by Length:
Java
// Java Program to Customize the Ordering // of Elements in a PriorityQueue import java.util.*; // Class representing a priority queue based on string length public class StringLengthPriorityQueue { // Main method public static void main(String[] args) { // Create a priority queue of strings with custom comparator PriorityQueue<String> pq = new PriorityQueue<>( 5 , new StringLengthComparator()); // Add strings to the priority queue pq.add( "apple" ); pq.add( "banana" ); pq.add( "grape" ); pq.add( "kiwi" ); // Print the strings served in order of length System.out.println( "Strings served in order of length:" ); while (!pq.isEmpty()) { System.out.println(pq.poll()); } } } // Comparator class for comparing strings based on their length class StringLengthComparator implements Comparator<String> { // Compare method to compare two strings based on their length public int compare(String s1, String s2) { return Integer.compare(s1.length(), s2.length()); } } |
Strings served in order of length: kiwi apple grape banana
Explanation of the above Program:
- In this example, we create a PriorityQueue of Strings.
- The custom comparator ( StringLengthComparator ) compares strings based on their length.
- Strings with shorter lengths are served first.