????????Leetcode刷题时想用优先队列解决,在使用时碰到了怎么排序的问题。我首先给自定义的类Node81实现Comparator接口,重写了接口中的 compare() 方法。 然后定义了一个优先队列 PriorityQueue queue = new PriorityQueue<>();,代码如下,然后运行时就报错啦。
class Node81 implements Comparator<Node81>{
int row;
int count;
Node81(){}
Node81(int row, int count) {
this.row = row;
this.count = count;
}
@Override
public int compare(Node81 o1, Node81 o2) {
if (o1.count > o2.count) {
return 1;
}
if (o1.count < o2.count) {
return -1;
}
return o1.row > o2.row ? 1 : -1;
}
}
public int[] kWeakestRows(int[][] mat, int k) {
PriorityQueue<Node81> queue = new PriorityQueue<>();
int n = mat.length, m = mat[0].length;
for (int i = 0; i < mat.length; i++) {
int count = 0;
for (int j = 0; j < mat[i].length; j++) {
if (mat[i][j] == 1) {
++count;
}
}
Node81 node81 = new Node81(i, count);
queue.add(node81);
}
int[] ans = new int[k];
for (int i = 0; i < k; i++) {
Node81 remove = queue.remove();
ans[i] = remove.row;
}
return ans;
}

?????经过调试后,发现这里我犯了一个低级错误,我虽然实现了Comparator接口,但是在申明初始化优先队列( PriorityQueue queue = new PriorityQueue<>();)的时候并没有将比较器传进去,所以在优先队列添加节点时,因为 compartor 为空会进入 下图中的 siftUpComparable() 方法,因为我没有给Node81类实现 **Comparable **接口中的 compareTo() 方法,就会报上图中的错误。 
Comparable 接口
public interface Comparable<T> {
public int compareTo(T o);
}
? ? ? ?可以看到,该接口中其实就只有一个 compareTo(T o) 的方法,将此对象与指定的对象进行顺序比较。Comparable表示可被排序的,实现该接口的类的对象自动拥有排序功能,像Collections.sort()或Arrays.sort可以直接对游该类组成的数组或者集合进行排序.
Comparator 接口
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
? ? ? ?Comparator是比较器,它可以作为一个参数传递到Collections.sort和Arrays.sort方法等来指定某个类对象的排序方式。像优先队列就可以在初始化的时候给其传递一个实现了Camparator接口的类。如下方代码所示。
public PriorityQueue(Comparator<? super E> comparator) {
this(DEFAULT_INITIAL_CAPACITY, comparator);
}
? ? ? ?所以我开始给优先队列初始化应该给其加上参数:PriorityQueue queue = new PriorityQueue<>(new Node81()); 这样优先队列就会识别到有比较强而不去找compareTo() 方法。从这里也可以看出:当Comparable和Comparator接口都实现了的时候,会优先使用Comparator接口的 compare() 方法来进行排序。
总结
- Comparable接口和Comparator的功能都差不多,都是给类排序
- Comparable是让类能够像基础数据一样自然排序,Comparator在使用时需要将其作为参数传递进去
- 当Comparable和Comparator接口同时都实现了的时候,会优先使用Comparator接口的排序规则
|