将 Java PriorityQueue 更改为最大 PQ

发布于 2024-09-19 07:44:42 字数 328 浏览 5 评论 0原文

Java 标准库中的优先级队列实现似乎是一个最小优先级队列,我发现这有点令人困惑。为了将其变成最大的,我创建了一个自定义比较器对象。

Comparator<Integer> cmp = new Comparator<Integer>()
{
    public int compare( Integer x, Integer y )
    {
        return y - x;
    }
};

我想知道是否有更优雅的解决方案。本质上我不想要一个可用于实现 Dijkstras 等的通用优先级队列。我什至没有意识到会有反向操作的队列:/

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;
    }
};

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 :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情深如许 2024-09-26 07:44:43

这是使用 Collections.reverseOrder() 的代码片段 -

    PriorityQueue<Integer> maxPQ = new PriorityQueue<Integer>(20,Collections.reverseOrder());

您还需要提供优先级队列的初始容量(此处为 20)以及比较器。

Here is a code snippet using Collections.reverseOrder()-

    PriorityQueue<Integer> maxPQ = new PriorityQueue<Integer>(20,Collections.reverseOrder());

You also need to provide the initial capacity of the Priority Queue (20 here) along with the Comparator.

终陌 2024-09-26 07:44:43

使用 Java 的 Collections.reverseOrder() 比较器。

Java 参考

Use Java's Collections.reverseOrder() comparator.

Java Reference

私野 2024-09-26 07:44:43

不确定你所说的优雅是什么意思,但是当我想要像 MaxHeap(在 Dijkstra 中使用)那样实现 PQ 时,我只使用内联比较器构造函数。

PriorityQueue<Integer> PQ= new PriorityQueue<Integer>(20, new Comparator<Integer>(){
            public int compare(Integer o1, Integer o2){
                return o2 - o1;
            }
        });

它足够简单,适合任何时候我正在寻找简单的东西并且只想使用比较器一次的情况。

Not sure what you mean by elegant but when I want a PQ implemented like a MaxHeap (used in Dijkstra's) I just use an inline comparator constructor.

PriorityQueue<Integer> PQ= new PriorityQueue<Integer>(20, new Comparator<Integer>(){
            public int compare(Integer o1, Integer o2){
                return o2 - o1;
            }
        });

It's simple enough for anytime I'm looking for something simple and only want to use the Comparator once.

天暗了我发光 2024-09-26 07:44:43

如果您有一个现有的比较器,您可以创建一个通用的反相比较器。

public class InverseComparator<T> implements Comparator<T> {
    private final Comparator<T> delegate;

    public InverseComparator(Comparator<T> delegate) {
        this.delegate = delegate;
    }

    public int compare(T x, T y) {
        return delegate(y, x);
    }
}

If you have an existing comparator you could create a generic inversing comparator.

public class InverseComparator<T> implements Comparator<T> {
    private final Comparator<T> delegate;

    public InverseComparator(Comparator<T> delegate) {
        this.delegate = delegate;
    }

    public int compare(T x, T y) {
        return delegate(y, x);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文