包含“对”的优先级队列以相反的数字顺序 Java
我正在尝试创建一个优先级队列,使其元素(整数对)保持与自然顺序相反的顺序。我在网站上找到了一些提示,但在每种情况下它都给出了相同的错误顺序。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自
PriorityQueue.iterator()
的文档:如果您想按优先顺序取出它们,请继续调用
poll()
直到返回null
:这将打印 58, 55, 23, 15,正如您正在寻找的那样。
From the docs for
PriorityQueue.iterator()
:If you want to get them out in priority order, keep calling
poll()
until it returnsnull
:That prints 58, 55, 23, 15, as you were looking for.
而不是“返回 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));