Java PriorityQueue not polling expected object
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 开发者_Python百科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 falseHow 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?
In case everything else is right (no concurrent pushes, not getting the test wrong), I could imagine two reasons:
- the objects change in a way affecting their order (forbidden)
- the comparator is wrong (not transitive or whatever)
精彩评论