包含“对”的优先级队列以相反的数字顺序 Java

发布于 2024-10-26 10:59:02 字数 1601 浏览 0 评论 0原文

我正在尝试创建一个优先级队列,使其元素(整数对)保持与自然顺序相反的顺序。我在网站上找到了一些提示,但在每种情况下它都给出了相同的错误顺序。

    PriorityQueue<Pair> pq = new PriorityQueue(4,
            new Comparator<Pair>() {
                public int compare(Pair a1, Pair a2) {
                    return a2.value.compareTo(a1.value);
                }
    });
    pq.add(new Pair(1,15));
    pq.add(new Pair(2,58));
    pq.add(new Pair(3,55));
    pq.add(new Pair(7,23));
    Iterator<Pair> it = pq.iterator();
    while(it.hasNext()) {
        System.out.println(it.next().value);
    }

这是 Pair 类

public class Pair implements Comparable {
public Integer name;
public Integer value;
public Pair(int name, int value) {
    this.name = name;
    this.value = value;

}
public int getname(){
    return name;
}    
public int getvalue() {
    return value;
}

public int compare(Pair o1, Pair o2) {
    Pair a1 = (Pair)o1;
    Pair a2 = (Pair)o2;
    if(a1.value>a2.value) {
        return 1;
    }
    else if(a1.value<a2.value) {
        return -1;
    }
    return 0;

}

@Override
public int hashCode() {
    int hash = 3;
    return hash;
}
@Override
public boolean equals(Object o) {
    Pair a2 = (Pair)o;
    return this.name == a2.name && this.value == a2.value;
}
public int compareTo(Object o) {
    Pair a2 = (Pair)o;
    if(this.value>a2.value) {
        return 1;
    }
    else if(this.value<a2.value) {
        return -1;
    }
    return 0;

}

}

如果我使用“new PriorityQueue()”构造函数,它会给出正确的自然顺序。 感谢您抽出时间, 标记

I'm trying to create a priorityqueue which keeps its elements(Pairs of integers) in the reverse of the natural order. I've found severeal tips on the site, but in every case it gave the same wrong order.

    PriorityQueue<Pair> pq = new PriorityQueue(4,
            new Comparator<Pair>() {
                public int compare(Pair a1, Pair a2) {
                    return a2.value.compareTo(a1.value);
                }
    });
    pq.add(new Pair(1,15));
    pq.add(new Pair(2,58));
    pq.add(new Pair(3,55));
    pq.add(new Pair(7,23));
    Iterator<Pair> it = pq.iterator();
    while(it.hasNext()) {
        System.out.println(it.next().value);
    }

Here is the Pair class

public class Pair implements Comparable {
public Integer name;
public Integer value;
public Pair(int name, int value) {
    this.name = name;
    this.value = value;

}
public int getname(){
    return name;
}    
public int getvalue() {
    return value;
}

public int compare(Pair o1, Pair o2) {
    Pair a1 = (Pair)o1;
    Pair a2 = (Pair)o2;
    if(a1.value>a2.value) {
        return 1;
    }
    else if(a1.value<a2.value) {
        return -1;
    }
    return 0;

}

@Override
public int hashCode() {
    int hash = 3;
    return hash;
}
@Override
public boolean equals(Object o) {
    Pair a2 = (Pair)o;
    return this.name == a2.name && this.value == a2.value;
}
public int compareTo(Object o) {
    Pair a2 = (Pair)o;
    if(this.value>a2.value) {
        return 1;
    }
    else if(this.value<a2.value) {
        return -1;
    }
    return 0;

}

}

If I use the "new PriorityQueue()" constructor, it gives a proper natural ordering.
Thanks for your time,
Mark

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

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

发布评论

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

评论(2

风轻花落早 2024-11-02 10:59:04

来自 PriorityQueue.iterator() 的文档:

返回对此队列中元素的迭代器。迭代器不按任何特定顺序返回元素。

如果您想按优先顺序取出它们,请继续调用 poll() 直到返回 null

Pair pair;
while((pair = pq.poll()) != null) {
    System.out.println(pair.value);
}

这将打印 58, 55, 23, 15,正如您正在寻找的那样。

From the docs for PriorityQueue.iterator():

Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

If you want to get them out in priority order, keep calling poll() until it returns null:

Pair pair;
while((pair = pq.poll()) != null) {
    System.out.println(pair.value);
}

That prints 58, 55, 23, 15, as you were looking for.

各自安好 2024-11-02 10:59:04

而不是“返回 a2.value.compareTo(a1.value);”
你应该直接使用 ((a2.value > a1.value)? 1: ((a2.value ==a1.value) ? 0 : -1));

instead of "return a2.value.compareTo(a1.value);"
you should directly use ((a2.value > a1.value)? 1: ((a2.value ==a1.value) ? 0 : -1));

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