Skip to content

Commit 170f001

Browse files
author
Rajeev Kumar Singh
committed
Added more examples
1 parent 0f93aa5 commit 170f001

File tree

3 files changed

+57
-27
lines changed

3 files changed

+57
-27
lines changed

java-priority-queue-examples/src/CreatePriorityQueueExample.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
public class CreatePriorityQueueExample {
44
public static void main(String[] args) {
55
// Create a Priority Queue
6-
PriorityQueue<String> namePriorityQueue = new PriorityQueue<>();
6+
PriorityQueue<Integer> numbers = new PriorityQueue<>();
77

88
// Add items to a Priority Queue (ENQUEUE)
9-
namePriorityQueue.add("Lisa");
10-
namePriorityQueue.add("Robert");
11-
namePriorityQueue.add("John");
12-
namePriorityQueue.add("Chris");
13-
namePriorityQueue.add("Angelina");
14-
namePriorityQueue.add("Joe");
9+
numbers.add(750);
10+
numbers.add(500);
11+
numbers.add(900);
12+
numbers.add(100);
1513

1614
// Remove items from the Priority Queue (DEQUEUE)
17-
while (!namePriorityQueue.isEmpty()) {
18-
System.out.println(namePriorityQueue.remove());
15+
while (!numbers.isEmpty()) {
16+
System.out.println(numbers.remove());
1917
}
2018

2119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.PriorityQueue;
2+
3+
public class CreatePriorityQueueStringExample {
4+
public static void main(String[] args) {
5+
// Create a Priority Queue
6+
PriorityQueue<String> namePriorityQueue = new PriorityQueue<>();
7+
8+
// Add items to a Priority Queue (ENQUEUE)
9+
namePriorityQueue.add("Lisa");
10+
namePriorityQueue.add("Robert");
11+
namePriorityQueue.add("John");
12+
namePriorityQueue.add("Chris");
13+
namePriorityQueue.add("Angelina");
14+
namePriorityQueue.add("Joe");
15+
16+
// Remove items from the Priority Queue (DEQUEUE)
17+
while (!namePriorityQueue.isEmpty()) {
18+
System.out.println(namePriorityQueue.remove());
19+
}
20+
21+
}
22+
}

java-priority-queue-examples/src/PriorityQueueUserDefinedObjectExample.java

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import java.util.Objects;
22
import 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

6565
public 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

Comments
 (0)