PriorityQueue<Integer> queue = new PriorityQueue<Integer>(4);
queue.add(8);
queue.add(5);
queue.add(23);
queue.add(6);
System.out.println(queue);
Friends,
The preceding code on Ubuntu 12.10 and Oracle Java 1.6 and Java 1.7, it is printing output as
[5, 6, 23, 8]
I believe this is wrong. This should have instead printed as [5, 6, 8, 23]
Is this a defect? or my understanding of priority queue is wrong?
In addition to it, if I change the position of adding 23 to PriorityQueue before or after, this works as expected.
The
toString()method for theAbstractCollectionclass (which is whatPriorityQueueuses) states that it:If you look at the docs for the PriorityQueue iterator, it states:
The priority only comes in to play when you extract items from the queue, not when you get a string representation of them.