Priority Queue
• provides the functionality of the heap data structure.
• It implements the Queue interface.
• elements are retrieved in sorted order.
• the head of the priority queue will be the smallest element.
the elements of a priority queue may not be sorted. However, elements are always retrieved in
sorted order.
Creating PriorityQueue
import the [Link] package.
Queue<Integer> numbers = new PriorityQueue<>();
Operations on PriorityQueue
1. Adding Elements:
Queue<Integer> pq = new PriorityQueue<>();
for(int i=0;i<3;i++)
{
[Link](i);
}
2. Removing Elements:
[Link](2);
[Link]("After Remove - " + pq);
[Link]("Poll Method - " + [Link]());
[Link]("Final PriorityQueue - " + pq);
3. Accessing the elements:
String element = [Link]();
[Link]("Accessed Element: " + element);
4. Iterating the PriorityQueue:
Iterator iterator = [Link]();
while ([Link]()) {
[Link]([Link]() + " ");
}
it uses a default natural ordering. In this case, it gave us the data back in the ascending
order.
5. Custom ordering using Comparator:
Custom ordering is possible with the help of a comparator.
static class CustomIntegerComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o1 < o2 ? 1 : -1;
}
}
to add this comparator to the priority queue.
public class Priority_Queue_Demo {
public static void main(String[] args)
{
PriorityQueue<Integer> PQ = new PriorityQueue<>(new CustomIntegerComparator());
[Link](11);
[Link](5);
[Link](-1);
[Link](12);
[Link](6);
[Link]("Integers stored in reverse order of priority in a Priority Queue\n");
while (![Link]()) {
[Link]([Link]());
}
}
output of the above program:
12
11
6
5
-1
6. Custom ordering using Comparable interface:
import [Link];
import [Link];
class Employee implements Comparable<Employee> {
private String name;
private double salary;
public Employee(String name, double salary) {
[Link] = name;
[Link] = salary;
public String getName() {
return name;
public void setName(String name) {
[Link] = name;
public double getSalary() {
return salary;
public void setSalary(double salary) {
[Link] = salary;
@Override
public boolean equals(Object e) {
if (this == e) return true;
if (e == null || getClass() != [Link]()) return false;
Employee employee = (Employee) e;
return [Link]([Link], salary) == 0 &&
[Link](name, [Link]);
@Override
public int hashCode() {
return [Link](name, salary);
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
// Compare two employee objects by their salary
@Override
public int compareTo(Employee employee) {
if([Link]() > [Link]()) {
return 1;
} else if ([Link]() < [Link]()) {
return -1;
} else {
return 0;
public class PriorityQueueUserDefinedObjectExample {
public static void main(String[] args) {
// Create a PriorityQueue
PriorityQueue<Employee> employeePriorityQueue = new PriorityQueue<>();
// Add items to the Priority Queue
[Link](new Employee("Rajeev", 100000.00));
[Link](new Employee("Chris", 145000.00));
[Link](new Employee("Andrea", 115000.00));
[Link](new Employee("Jack", 167000.00));
while (![Link]()) {
[Link]([Link]());
# Output
Employee{name='Rajeev', salary=100000.0}
Employee{name='Andrea', salary=115000.0}
Employee{name='Chris', salary=145000.0}
Employee{name='Jack', salary=167000.0}
Java Comparable and Comparator
Java Comparable
example of a Comparable interface that sorts the list elements on the basis of age.
import [Link].*;
import [Link].*;
class Student implements Comparable<Student>
{
int rollno;
String name;
int age;
Student(int rollno,String name,int age)
{
[Link]=rollno;
[Link]=name;
[Link]=age;
}
public int compareTo(Student st)
{
if(age==[Link])
return 0;
else if(age>[Link])
return 1;
else
return -1;
}
}
//Creating a test class to sort the elements
public class TestSort3
{
public static void main(String args[])
{
ArrayList<Student> al=new ArrayList<Student>();
[Link](new Student(101,"Vijay",23));
[Link](new Student(106,"Ajay",27));
[Link](new Student(105,"Jai",21));
[Link](al);
for(Student st:al)
{
[Link]([Link]+" "+[Link]+" "+[Link]);
}
}
}
Java Comparator
[Link]
class Student
{
int rollno;
String name;
int age;
Student(int rollno,String name,int age)
{
[Link]=rollno;
[Link]=name;
[Link]=age;
}
}
[Link]
import [Link].*;
class AgeComparator implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
if([Link]==[Link])
return 0;
else if([Link]>[Link])
return 1;
else
return -1;
}
}
[Link]
import [Link].*;
class NameComparator implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
return [Link]([Link]);
}
}
[Link]
import [Link].*;
import [Link].*;
class TestComparator{
public static void main(String args[])
{
//Creating a list of students
ArrayList<Student> al=new ArrayList<Student>();
[Link](new Student(101,"Vijay",23));
[Link](new Student(106,"Ajay",27));
[Link](new Student(105,"Jai",21));
[Link]("Sorting by Name");
//Using NameComparator to sort the elements
[Link](al,new NameComparator());
//Traversing the elements of list
for(Student st: al){
[Link]([Link]+" "+[Link]+" "+[Link]);
}
[Link]("sorting by Age");
//Using AgeComparator to sort the elements
[Link](al,new AgeComparator());
//Travering the list again
for(Student st: al){
[Link]([Link]+" "+[Link]+" "+[Link]);
}
}
}
Output:
Sorting by Name
106 Ajay 27
105 Jai 21
101 Vijay 23
Sorting by Age
105 Jai 21
101 Vijay 23
106 Ajay 27