Priority Queue, Comparator, Comparable - Notes
Priority Queue, Comparator, Comparable - Notes
Creating PriorityQueue
import the java.util.PriorityQueue package.
Queue<Integer> numbers = new PriorityQueue<>();
Operations on PriorityQueue
1. Adding Elements:
Queue<Integer> pq = new PriorityQueue<>();
for(int i=0;i<3;i++)
{
pq.add(i);
}
2. Removing Elements:
pq.remove(2);
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;
}
}
12
11
6
5
-1
import java.util.Objects;
import java.util.PriorityQueue;
this.name = name;
this.salary = salary;
return name;
this.name = name;
return salary;
this.salary = salary;
@Override
Objects.equals(name, employee.name);
@Override
}
@Override
return "Employee{" +
'}';
@Override
return 1;
return -1;
} else {
return 0;
// Create a PriorityQueue
while (!employeePriorityQueue.isEmpty()) {
System.out.println(employeePriorityQueue.remove());
# 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}
import java.util.*;
import java.io.*;
class Student implements Comparable<Student>
{
int rollno;
String name;
int age;
Student(int rollno,String name,int age)
{
this.rollno=rollno;
this.name=name;
this.age=age;
}
public int compareTo(Student st)
{
if(age==st.age)
return 0;
else if(age>st.age)
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>();
al.add(new Student(101,"Vijay",23));
al.add(new Student(106,"Ajay",27));
al.add(new Student(105,"Jai",21));
Collections.sort(al);
for(Student st:al)
{
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
Java Comparator
Student.java
class Student
{
int rollno;
String name;
int age;
Student(int rollno,String name,int age)
{
this.rollno=rollno;
this.name=name;
this.age=age;
}
}
AgeComparator.java
import java.util.*;
class AgeComparator implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
if(s1.age==s2.age)
return 0;
else if(s1.age>s2.age)
return 1;
else
return -1;
}
}
NameComparator.java
import java.util.*;
class NameComparator implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
return s1.name.compareTo(s2.name);
}
}
TestComparator.java
import java.util.*;
import java.io.*;
class TestComparator{
public static void main(String args[])
{
//Creating a list of students
ArrayList<Student> al=new ArrayList<Student>();
al.add(new Student(101,"Vijay",23));
al.add(new Student(106,"Ajay",27));
al.add(new Student(105,"Jai",21));
System.out.println("Sorting by Name");
//Using NameComparator to sort the elements
Collections.sort(al,new NameComparator());
//Traversing the elements of list
for(Student st: al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
System.out.println("sorting by Age");
//Using AgeComparator to sort the elements
Collections.sort(al,new AgeComparator());
//Travering the list again
for(Student st: al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
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