基础概念:
比较器(Comparator)是Java中的一个接口,用于定义自定义的排序规则。它主要用于对集合(如List、Set)或数组进行排序。通过实现compare(T o1, T o2)
方法,可以指定两个对象之间的比较逻辑。
优势:
类型:
Comparable
接口,在类内部定义排序规则。Comparator
接口,在类外部定义排序规则。应用场景:
示例代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
public class ComparatorExample {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 35));
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return p1.age - p2.age;
}
});
for (Person person : people) {
System.out.println(person);
}
}
}
基础概念:
PriorityQueue是Java中的一个优先队列实现,它是一个基于优先级堆的无界优先级队列。元素按照自然顺序(如果实现了Comparable
接口)或通过构造函数提供的Comparator
进行排序。
优势:
类型:
Comparable
接口。Comparator
对象。应用场景:
示例代码:
import java.util.PriorityQueue;
class Task implements Comparable<Task> {
int priority;
String description;
public Task(int priority, String description) {
this.priority = priority;
this.description = description;
}
@Override
public int compareTo(Task other) {
return Integer.compare(this.priority, other.priority);
}
@Override
public String toString() {
return "Task{priority=" + priority + ", description='" + description + "'}";
}
}
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Task> tasks = new PriorityQueue<>();
tasks.add(new Task(3, "Low priority task"));
tasks.add(new Task(1, "High priority task"));
tasks.add(new Task(2, "Medium priority task"));
while (!tasks.isEmpty()) {
System.out.println(tasks.poll());
}
}
}
问题:为什么PriorityQueue中的元素顺序不对? 原因:
Comparable
接口。Comparator
实现有误。解决方法:
Comparable
接口。Comparator
的实现逻辑。示例代码(错误的Comparator):
import java.util.PriorityQueue;
class Task {
int priority;
String description;
public Task(int priority, String description) {
this.priority = priority;
this.description = description;
}
@Override
public String toString() {
return "Task{priority=" + priority + ", description='" + description + "'}";
}
}
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Task> tasks = new PriorityQueue<>((t1, t2) -> t2.priority - t1.priority);
tasks.add(new Task(3, "Low priority task"));
tasks.add(new Task(1, "High priority task"));
tasks.add(new Task(2, "Medium priority task"));
while (!tasks.isEmpty()) {
System.out.println(tasks.poll());
}
}
}
解决方法:
import java.util.PriorityQueue;
class Task implements Comparable<Task> {
int priority;
String description;
public Task(int priority, String description) {
this.priority = priority;
this.description = description;
}
@Override
public int compareTo(Task other) {
return Integer.compare(this.priority, other.priority);
}
@Override
public String toString() {
return "Task{priority=" + priority + ", description='" + description + "'}";
}
}
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Task> tasks = new PriorityQueue<>();
tasks.add(new Task(3, "Low priority task"));
tasks.add(new Task(1, "High priority task"));
tasks.add(new Task(2, "Medium priority task"));
while (!tasks.isEmpty()) {
System.out.println(tasks.poll());
}
}
}
通过以上示例代码和解释,可以更好地理解比较器和PriorityQueue在Java中的工作原理及其应用场景。
领取专属 10元无门槛券
手把手带您无忧上云