Ari*_*Ari 1 java priority-queue comparator
我有一个Java PriorityQueue用于从我称为Node的特定类中对对象进行排序.我希望它通过getData()方法对节点进行排序.我尝试了以下代码(使用比较器),但它没有用.当我调用优先级队列的"poll"方法时,它首先没有返回最低结果,而是以看似随机的顺序返回.我如何解决它?谢谢!
PriorityQueue<Node> pq = new PriorityQueue<Node>(hm.size(),
new Comparator<Node>( ) {
// override the compare method
public int compare(Node i, Node j) {
if (i.getData()<j.getData()){
return i.getData(); //It should sort by the Node's getData method.
}
return j.getData();
Run Code Online (Sandbox Code Playgroud)