【发布时间】:2011-08-07 09:23:15
【问题描述】:
我有一个优先级队列,我在其中添加了一个节点对象,节点应该按它们包含的值排序。由于某种原因,优先级队列不会在添加时对节点进行排序。如果有人对此有任何问题或有任何指导,我将不胜感激。下面是一个简单的例子:
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;
}
【问题讨论】:
标签: java sorting priority-queue