我有一个优先级队列,我在其中添加了一个Node对象,其中节点应该按它们包含的值进行排序。由于某些原因,优先级队列不会在添加时对节点进行排序。如果任何人能看到这方面的错误或有任何指导,我将不胜感激。下面是一个简单的示例:
PriorityQueue<Node> PQ = new PriorityQueue<Node>();
//for each entry create a node and add it to the PriorityQueue
for(Entry<Character,Integer> entry : entries){
PQ.add(new Node(entry.getKey(),entry.getValue(), true));
}
下面是节点的compareTo
方法:
@Override
public int compareTo(Node n) {
if(n.frequency.intValue() > this.frequency.intValue()) return -1;
else if(n.frequency.intValue() == this.frequency.intValue()) return 0;
else return 1;
}
https://stackoverflow.com/questions/5695017
复制相似问题