一、创建一个PriorityQueue实例
import java.util.PriorityQueue;
public class CreatePriorityQueueExample {
public static void main(String[] args) {
// Create a Priority Queue
PriorityQueue<Integer> numbers = new PriorityQueue<>();
// Add items to a Priority Queue (ENQUEUE)
numbers.add(750);
numbers.add(500);
numbers.add(900);
numbers.add(100);
// Remove items from the Priority Queue (DEQUEUE)
while (!numbers.isEmpty()) {
System.out.println(numbers.remove());
}
}
}
# Output
100
500
750
900
二、创建一个String类型的PriorityQueue实例
import java.util.PriorityQueue;
public class CreatePriorityQueueStringExample {
public static void main(String[] args) {
// Create a Priority Queue
PriorityQueue<String> namePriorityQueue = new PriorityQueue<>();
// Add items to a Priority Queue (ENQUEUE)
namePriorityQueue.add("Lisa");
namePriorityQueue.add("Robert");
namePriorityQueue.add("John");
namePriorityQueue.add("Chris");
namePriorityQueue.add("Angelina");
namePriorityQueue.add("Joe");
// Remove items from the Priority Queue (DEQUEUE)
while (!namePriorityQueue.isEmpty()) {
System.out.println(namePriorityQueue.remove());
}
}
}
# Output
Angelina
Chris
Joe
John
Lisa
Robert
三、使用自定义的Comparator
import java.util.Comparator;
import java.util.PriorityQueue;
public class PriorityQueueCustomComparatorExample {
public static void main(String[] args) {
// A custom comparator that compares two Strings by their length.
Comparator<String> stringLengthComparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
};
/*
The above Comparator can also be created using lambda expression like this =>
Comparator<String> stringLengthComparator = (s1, s2) -> {
return s1.length() - s2.length();
};
Which can be shortened even further like this =>
Comparator<String> stringLengthComparator = Comparator.comparingInt(String::length);
*/
// Create a Priority Queue with a custom Comparator
PriorityQueue<String> namePriorityQueue = new PriorityQueue<>(stringLengthComparator);
// Add items to a Priority Queue (ENQUEUE)
namePriorityQueue.add("Lisa");
namePriorityQueue.add("Robert");
namePriorityQueue.add("John");
namePriorityQueue.add("Chris");
namePriorityQueue.add("Angelina");
namePriorityQueue.add("Joe");
// Remove items from the Priority Queue (DEQUEUE)
while (!namePriorityQueue.isEmpty()) {
System.out.println(namePriorityQueue.remove());
}
}
}
# Output
Joe
John
Lisa
Chris
Robert
Angelina
四、创建一个自定义类型的PriorityQueue实例
import java.util.Objects;
import java.util.PriorityQueue;
class Employee implements Comparable<Employee> {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Double.compare(employee.salary, salary) == 0 &&
Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(name, salary);
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
// Compare two employee objects by their salary
@Override
public int compareTo(Employee employee) {
if(this.getSalary() > employee.getSalary()) {
return 1;
} else if (this.getSalary() < employee.getSalary()) {
return -1;
} else {
return 0;
}
}
}
public class PriorityQueueUserDefinedObjectExample {
public static void main(String[] args) {
/*
The requirement for a PriorityQueue of user defined objects is that
1. Either the class should implement the Comparable interface and provide
the implementation for the compareTo() function.
2. Or you should provide a custom Comparator while creating the PriorityQueue.
*/
// Create a PriorityQueue
PriorityQueue<Employee> employeePriorityQueue = new PriorityQueue<>();
// Add items to the Priority Queue
employeePriorityQueue.add(new Employee("Rajeev", 100000.00));
employeePriorityQueue.add(new Employee("Chris", 145000.00));
employeePriorityQueue.add(new Employee("Andrea", 115000.00));
employeePriorityQueue.add(new Employee("Jack", 167000.00));
/*
The compareTo() method implemented in the Employee class is used to determine
in what order the objects should be dequeued.
*/
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}