11import java .util .Objects ;
22import java .util .PriorityQueue ;
33
4- class Person implements Comparable <Person > {
4+ class Employee implements Comparable <Employee > {
55 private String name ;
66 private double salary ;
77
8- public Person (String name , double salary ) {
8+ public Employee (String name , double salary ) {
99 this .name = name ;
1010 this .salary = salary ;
1111 }
@@ -30,9 +30,9 @@ public void setSalary(double salary) {
3030 public boolean equals (Object o ) {
3131 if (this == o ) return true ;
3232 if (o == null || getClass () != o .getClass ()) return false ;
33- Person person = (Person ) o ;
34- return Double .compare (person .salary , salary ) == 0 &&
35- Objects .equals (name , person .name );
33+ Employee employee = (Employee ) o ;
34+ return Double .compare (employee .salary , salary ) == 0 &&
35+ Objects .equals (name , employee .name );
3636 }
3737
3838 @ Override
@@ -42,18 +42,18 @@ public int hashCode() {
4242
4343 @ Override
4444 public String toString () {
45- return "Person {" +
45+ return "Employee {" +
4646 "name='" + name + '\'' +
4747 ", salary=" + salary +
4848 '}' ;
4949 }
5050
51- // Compare two person objects by their salary
51+ // Compare two employee objects by their salary
5252 @ Override
53- public int compareTo (Person person ) {
54- if (this .getSalary () > person .getSalary ()) {
53+ public int compareTo (Employee employee ) {
54+ if (this .getSalary () > employee .getSalary ()) {
5555 return 1 ;
56- } else if (this .getSalary () < person .getSalary ()) {
56+ } else if (this .getSalary () < employee .getSalary ()) {
5757 return -1 ;
5858 } else {
5959 return 0 ;
@@ -64,20 +64,30 @@ public int compareTo(Person person) {
6464
6565public class PriorityQueueUserDefinedObjectExample {
6666 public static void main (String [] args ) {
67- PriorityQueue <Person > personPriorityQueue = new PriorityQueue <>();
67+ /*
68+ The requirement for a PriorityQueue of user defined objects is that
69+
70+ 1. Either the class should implement the Comparable interface and provide
71+ the implementation for the compareTo() function.
72+ 2. Or you should provide a custom Comparator while creating the PriorityQueue.
73+ */
74+ 75+ // Create a PriorityQueue
76+ PriorityQueue <Employee > employeePriorityQueue = new PriorityQueue <>();
6877
6978 // Add items to the Priority Queue
70- personPriorityQueue .add (new Person ("Rajeev" , 100000.00 ));
71- personPriorityQueue .add (new Person ("Chris" , 145000.00 ));
72- personPriorityQueue .add (new Person ("Andrea" , 115000.00 ));
73- personPriorityQueue .add (new Person ("Jack" , 167000.00 ));
79+ employeePriorityQueue .add (new Employee ("Rajeev" , 100000.00 ));
80+ employeePriorityQueue .add (new Employee ("Chris" , 145000.00 ));
81+ employeePriorityQueue .add (new Employee ("Andrea" , 115000.00 ));
82+ employeePriorityQueue .add (new Employee ("Jack" , 167000.00 ));
7483
7584
7685 /*
77- The compare() function implemented in the Person class is used to determine in what order the objects should be dequeued.
86+ The compareTo() method implemented in the Employee class is used to determine
87+ in what order the objects should be dequeued.
7888 */
79- while (!personPriorityQueue .isEmpty ()) {
80- System .out .println (personPriorityQueue .remove ());
89+ while (!employeePriorityQueue .isEmpty ()) {
90+ System .out .println (employeePriorityQueue .remove ());
8191 }
8292 }
8393}
0 commit comments