将Java PriorityQueue更改为Max PQ [英] Changing Java PriorityQueue to a Max PQ
本文介绍了将Java PriorityQueue更改为Max PQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Java标准库中的Priority Queue实现似乎是最小优先级队列,我发现有些令人困惑。为了将它变成最大值,我创建了一个自定义比较器对象。
The Priority Queue implementation in the Java standard library appears to be a min Priority Queue which I found somewhat confusing. In order to turn it into a max one I created a custom comparator object.
Comparator<Integer> cmp = new Comparator<Integer>()
{
public int compare( Integer x, Integer y )
{
return y - x;
}
};
我想知道是否有更优雅的解决方案。基本上我不是一个可用于实现Dijkstras等的通用优先级队列。我甚至没有意识到会有反向操作的那些:/
I was wondering if there was a more elegant solution. Essentially I wan't a generic priority queue that could be used to implement Dijkstras etc. I didn't even realise there would be ones which operated in reverse :/
推荐答案
使用Java的 Collections.reverseOrder()
比较器。
Use Java's Collections.reverseOrder()
comparator.
这篇关于将Java PriorityQueue更改为Max PQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文