Java PriorityQueue 和 Comparator 排序不正确
Java PriorityQueue and Comparator Not Ordering correctly
我是 Java 的新手,正在尝试使用自定义比较器实现优先级队列。我想将句子放入队列中并移除它们以获得最高分。
对于比较器 class 我有:
public class SentenceScoreComparator implements Comparator<Sentence> {
@Override
public int compare(Sentence o1, Sentence o2) {
if (o2.getScore() > o1.getScore()) return -1;
//fixed typo
if (o2.getScore() < o1.getScore()) return 1;
return 0;
}
}
然后我打印出这样的句子:
PriorityQueue<Sentence> allSentences = new PriorityQueue<Sentence>(new SentenceScoreComparator());
//add sentences
for(Sentence s :allSentences){
System.out.println(s.getScore());
}
但顺序不对
0.34432960587450223
0.47885099912108975
0.10991840331015199
0.36222267254836954
0.05164923572003221
0.5366117828694823
0.3891453014131773
0.0961512261934429
0.5566040852233918
0.5079687049927742
0.7628021620154812
0.6023121606121791
0.25695632228681914
0.15701049878801304
0.1260031244674359
0.36516025683986736
0.3846995962155155
我检查了队列是否使用了具有正确比较器方法的比较器。有人可以解释一下我错过了什么吗?
PriorityQueue
从未承诺过按顺序遍历它们。来自 javadocs:
This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).
您在第二个 if
中的比较器中有错字,其中 o2
分数与自身进行比较。
替换为:
@Override
public int compare(Sentence o1, Sentence o2) {
return Double.compare(o1.getScore(), o2.getScore());
}
最重要的是,正如 bradimus 回答的那样,PriorityQueue
不保证任何排序的遍历。使用常规列表并为此排序。
我是 Java 的新手,正在尝试使用自定义比较器实现优先级队列。我想将句子放入队列中并移除它们以获得最高分。
对于比较器 class 我有:
public class SentenceScoreComparator implements Comparator<Sentence> {
@Override
public int compare(Sentence o1, Sentence o2) {
if (o2.getScore() > o1.getScore()) return -1;
//fixed typo
if (o2.getScore() < o1.getScore()) return 1;
return 0;
}
}
然后我打印出这样的句子:
PriorityQueue<Sentence> allSentences = new PriorityQueue<Sentence>(new SentenceScoreComparator());
//add sentences
for(Sentence s :allSentences){
System.out.println(s.getScore());
}
但顺序不对
0.34432960587450223
0.47885099912108975
0.10991840331015199
0.36222267254836954
0.05164923572003221
0.5366117828694823
0.3891453014131773
0.0961512261934429
0.5566040852233918
0.5079687049927742
0.7628021620154812
0.6023121606121791
0.25695632228681914
0.15701049878801304
0.1260031244674359
0.36516025683986736
0.3846995962155155
我检查了队列是否使用了具有正确比较器方法的比较器。有人可以解释一下我错过了什么吗?
PriorityQueue
从未承诺过按顺序遍历它们。来自 javadocs:
This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).
您在第二个 if
中的比较器中有错字,其中 o2
分数与自身进行比较。
替换为:
@Override
public int compare(Sentence o1, Sentence o2) {
return Double.compare(o1.getScore(), o2.getScore());
}
最重要的是,正如 bradimus 回答的那样,PriorityQueue
不保证任何排序的遍历。使用常规列表并为此排序。