Prerequisite : Priority Queue, Comparator Priority Queue is like a regular queue, but each element has a “priority” associated with it. In a priority queue, an element with high priority is served before an element with low priority. For this, it uses a comparison function which imposes a total ordering of the elements.
The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used Constructors :
- public PriorityQueue() : This creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
- public PriorityQueue(Collection c) : This creates a PriorityQueue containing the elements in the specified collection(c). If the specified collection is an instance of a SortedSet, this priority queue will be ordered according to the same ordering, else this priority queue will be ordered according to the natural ordering of its elements.
- public PriorityQueue(int capacity, Comparator comparator) : This creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
Parameters: capacity - the initial capacity for this priority queue comparator - the comparator that will be used to order this priority queue. If null, the natural ordering of the elements will be used.
- public PriorityQueue(SortedSet ss) : Creates a PriorityQueue containing the elements in the specified sorted set. This priority queue will be ordered according to the same ordering as the given sorted set.
Sample code provided illustrates students with high priority(based on cgpa) are served before the students having low cgpa.
Implementation:
Java
// Java program to demonstrate working of // comparator based priority queue constructor import java.util.*; public class Example { public static void main(String[] args){ Scanner in = new Scanner(System.in); // Creating Priority queue constructor having // initial capacity=5 and a StudentComparator instance // as its parameters PriorityQueue<Student> pq = new PriorityQueue<Student>( 5 , new StudentComparator()); // Invoking a parameterized Student constructor with // name and cgpa as the elements of queue Student student1 = new Student("Nandini", 3.2 ); // Adding a student object containing fields // name and cgpa to priority queue pq.add(student1); Student student2 = new Student("Anmol", 3.6 ); pq.add(student2); Student student3 = new Student("Palak", 4.0 ); pq.add(student3); // Printing names of students in priority order,poll() // method is used to access the head element of queue System.out.println("Students served in their priority order"); while (!pq.isEmpty()) { System.out.println(pq.poll().getName()); } } } class StudentComparator implements Comparator<Student>{ // Overriding compare()method of Comparator // for descending order of cgpa 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 Student { public String name; public double cgpa; // A parameterized student constructor public Student(String name, double cgpa) { this .name = name; this .cgpa = cgpa; } public String getName() { return name; } } |
Students served in their priority order Palak Anmol Nandini
Note : This type of Priority queue is preferred in scenarios where customized ordering is required, i.e when one wants a different sorting order, then one can define its own way of comparing instances. Comparator can be implemented if there is a more complex comparing algorithm, e.g. multiple fields and so on.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!