1
1
import java .util .Objects ;
2
2
import java .util .PriorityQueue ;
3
3
4
- class Person implements Comparable <Person > {
4
+ class Employee implements Comparable <Employee > {
5
5
private String name ;
6
6
private double salary ;
7
7
8
- public Person (String name , double salary ) {
8
+ public Employee (String name , double salary ) {
9
9
this .name = name ;
10
10
this .salary = salary ;
11
11
}
@@ -30,9 +30,9 @@ public void setSalary(double salary) {
30
30
public boolean equals (Object o ) {
31
31
if (this == o ) return true ;
32
32
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 );
36
36
}
37
37
38
38
@ Override
@@ -42,18 +42,18 @@ public int hashCode() {
42
42
43
43
@ Override
44
44
public String toString () {
45
- return "Person {" +
45
+ return "Employee {" +
46
46
"name='" + name + '\'' +
47
47
", salary=" + salary +
48
48
'}' ;
49
49
}
50
50
51
- // Compare two person objects by their salary
51
+ // Compare two employee objects by their salary
52
52
@ 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 ()) {
55
55
return 1 ;
56
- } else if (this .getSalary () < person .getSalary ()) {
56
+ } else if (this .getSalary () < employee .getSalary ()) {
57
57
return -1 ;
58
58
} else {
59
59
return 0 ;
@@ -64,20 +64,30 @@ public int compareTo(Person person) {
64
64
65
65
public class PriorityQueueUserDefinedObjectExample {
66
66
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 <>();
68
77
69
78
// 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 ));
74
83
75
84
76
85
/*
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.
78
88
*/
79
- while (!personPriorityQueue .isEmpty ()) {
80
- System .out .println (personPriorityQueue .remove ());
89
+ while (!employeePriorityQueue .isEmpty ()) {
90
+ System .out .println (employeePriorityQueue .remove ());
81
91
}
82
92
}
83
93
}
0 commit comments