Skip to content

Commit f5c5a3d

Browse files
author
Rajeev Kumar Singh
committed
Priority Queue
1 parent 01fdb1f commit f5c5a3d

File tree

3 files changed

+131
-9
lines changed

3 files changed

+131
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Comparator;
2+
import java.util.PriorityQueue;
3+
4+
public class CreatePriorityQueueExample {
5+
public static void main(String[] args) {
6+
Comparator<String> stringLengthComparator = new Comparator<String>() {
7+
@Override
8+
public int compare(String s1, String s2) {
9+
return s1.length() - s2.length();
10+
}
11+
};
12+
13+
/*
14+
The above Comparator can also be created using lambda expression like this =>
15+
Comparator<String> stringLengthComparator = (s1, s2) -> {
16+
return s1.length() - s2.length();
17+
};
18+
19+
The above statement can be shortened even further like this =>
20+
Comparator<String> stringLengthComparator = Comparator.comparingInt(String::length);
21+
*/
22+
23+
// Create a Priority Queue with a custom Comparator
24+
PriorityQueue<String> namePriorityQueue = new PriorityQueue<>(stringLengthComparator);
25+
26+
// Add items to a Priority Queue (ENQUEUE)
27+
namePriorityQueue.add("Lisa");
28+
namePriorityQueue.add("Robert");
29+
namePriorityQueue.add("John");
30+
namePriorityQueue.add("Chris");
31+
namePriorityQueue.add("Angelina");
32+
namePriorityQueue.add("Joe");
33+
34+
// Remove items from the Priority Queue (DEQUEUE)
35+
while (!namePriorityQueue.isEmpty()) {
36+
System.out.println(namePriorityQueue.remove());
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.Objects;
2+
import java.util.PriorityQueue;
3+
4+
class Person implements Comparable<Person> {
5+
private String name;
6+
private double salary;
7+
8+
public Person(String name, double salary) {
9+
this.name = name;
10+
this.salary = salary;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public double getSalary() {
22+
return salary;
23+
}
24+
25+
public void setSalary(double salary) {
26+
this.salary = salary;
27+
}
28+
29+
@Override
30+
public boolean equals(Object o) {
31+
if (this == o) return true;
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);
36+
}
37+
38+
@Override
39+
public int hashCode() {
40+
return Objects.hash(name, salary);
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "Person{" +
46+
"name='" + name + '\'' +
47+
", salary=" + salary +
48+
'}';
49+
}
50+
51+
// Compare two person objects by their salary
52+
@Override
53+
public int compareTo(Person person) {
54+
if(this.getSalary() > person.getSalary()) {
55+
return 1;
56+
} else if (this.getSalary() < person.getSalary()) {
57+
return -1;
58+
} else {
59+
return 0;
60+
}
61+
}
62+
}
63+
64+
65+
public class PriorityQueueUserDefinedObjectExample {
66+
public static void main(String[] args) {
67+
PriorityQueue<Person> personPriorityQueue = new PriorityQueue<>();
68+
69+
// 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));
74+
75+
76+
/*
77+
The compare() function implemented in the Person class is used to determine in what order the objects should be dequeued.
78+
*/
79+
while (!personPriorityQueue.isEmpty()) {
80+
System.out.println(personPriorityQueue.remove());
81+
}
82+
}
83+
}

java-treeset-examples/src/TreeSetUserDefinedObjectExample.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
import java.util.TreeSet;
55

66
class Employee implements Comparable<Employee> {
7-
private Integer id;
7+
private int id;
88
private String name;
99

10-
public Employee(Integer id, String name) {
10+
public Employee(int id, String name) {
1111
this.id = id;
1212
this.name = name;
1313
}
1414

15-
public Integer getId() {
15+
public int getId() {
1616
return id;
1717
}
1818

19-
public void setId(Integer id) {
19+
public void setId(int id) {
2020
this.id = id;
2121
}
2222

@@ -34,7 +34,7 @@ public boolean equals(Object o) {
3434
if (this == o) return true;
3535
if (o == null || getClass() != o.getClass()) return false;
3636
Employee employee = (Employee) o;
37-
return Objects.equals(id, employee.id);
37+
return id == employee.id;
3838
}
3939

4040
@Override
@@ -45,7 +45,7 @@ public int hashCode() {
4545
// Compare employees based on their IDs
4646
@Override
4747
public int compareTo(Employee employee) {
48-
return this.getId().compareTo(employee.getId());
48+
return this.getId() - employee.getId();
4949
}
5050

5151
@Override
@@ -65,22 +65,22 @@ public static void main(String[] args) {
6565
/*
6666
The requirement for a TreeSet of user defined objects is that
6767
68-
1. Either the object should implement the Comparable interface and provide
68+
1. Either the class should implement the Comparable interface and provide
6969
the implementation for the compareTo() function.
7070
2. Or you should provide a custom Comparator while creating the TreeSet.
7171
*/
7272

7373
SortedSet<Employee> employees = new TreeSet<>();
7474

75-
// TreeSet uses the compareTo() method of the Book class to compare two books and sort them
75+
// TreeSet uses the compareTo() method of the Employee class to compare two employees and sort them
7676
employees.add(new Employee(1010, "Rajeev"));
7777
employees.add(new Employee(1005, "Sachin"));
7878
employees.add(new Employee(1008, "Chris"));
7979

8080
System.out.println("Employees (sorted based on Employee class's compareTo() function)");
8181
System.out.println(employees);
8282

83-
// Using a Custom Comparator
83+
// Providing a Custom Comparator (This comparator compares the employees based on their Name)
8484
employees = new TreeSet<>(Comparator.comparing(Employee::getName));
8585

8686
employees.add(new Employee(1010, "Rajeev"));

0 commit comments

Comments
 (0)