Java PriorityQueue 未轮询预期对象
这是我在这里发表的第一篇文章,所以请随时为我指出关于在这里提出问题的正确方向。
我的问题是 java.util.PriorityQueue。
我有一个初始化的队列;
myComparable comp = new myComparable();
PriorityQueue<someObject> prioritized = new PriorityQueue<someObject>(11, comp);
我认为我的队列中有什么或者 myComparable 是如何实现的问题并不重要。
然后我得到意外的输出:
prioritizedObject = prioritized.poll();
for(someObject otherObject : prioritized)
{
System.out.println(comp.compare(prioritizedObject, otherObject));
System.out.println(comp.equals(prioritizedObject, otherObject));
}
对于列表中的一个对象,此打印:
1
false
这怎么可能呢?当我使用的比较器表示队列中的另一个对象较小时,如何从队列中 poll() 一个对象?
This is my first post here so feel free to point me in the right direction regarding formulating a question here.
My issue is with the java.util.PriorityQueue.
I have a queue that I initialize;
myComparable comp = new myComparable();
PriorityQueue<someObject> prioritized = new PriorityQueue<someObject>(11, comp);
I dont think it matters for the question what is in my queue or how myComparable is implemented.
I then get unexpected output:
prioritizedObject = prioritized.poll();
for(someObject otherObject : prioritized)
{
System.out.println(comp.compare(prioritizedObject, otherObject));
System.out.println(comp.equals(prioritizedObject, otherObject));
}
For one object in the list this prints:
1
false
How can this be? How can I poll() an object from the queue while the comparator Im using says that another object in the queue is smaller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果其他一切都正确(没有并发推送,没有测试错误),我可以想象两个原因:
In case everything else is right (no concurrent pushes, not getting the test wrong), I could imagine two reasons: