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
import java.util.*;
public class Example {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
PriorityQueue<Student> pq = new
PriorityQueue<Student>( 5 , new StudentComparator());
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);
System.out.println("Students served in their priority order");
while (!pq.isEmpty()) {
System.out.println(pq.poll().getName());
}
}
}
class StudentComparator implements Comparator<Student>{
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;
public Student(String name, double cgpa) {
this .name = name;
this .cgpa = cgpa;
}
public String getName() {
return name;
}
}
|
Output:
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
05 Aug, 2022
Like Article
Save Article