| Author |
Priority Queue Clarification
|
Muthaiah Ramanathan
Ranch Hand
Joined: May 16, 2005
Posts: 102
|
|
I'm puzzled with the behaviour of the PriorityQueue. The executing the following code
prints out [0, 1, 2, 7, 5, 9, 3]
In the SUN API Definition, it says
An unbounded priority queue based on a priority heap. This queue orders elements according to an order specified at construction time, which is specified either according to their natural order (see Comparable), or according to a Comparator, depending on which constructor is used.
But why is the above not sorted according to the natural order? Or is it just the way the toString works?
|
SCJP 1.6, SCJD, SCWCD, SCBCD.
Be nice to people on the way up cos, you'll need 'em on your way down - From somewhere I can't remember!
|
 |
Muthaiah Ramanathan
Ranch Hand
Joined: May 16, 2005
Posts: 102
|
|
BTW - changing it to String also results in the same.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
This problem has been asked multiple times. See this or this...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Brij Garg
Ranch Hand
Joined: Apr 29, 2008
Posts: 234
|
|
But why is the above not sorted according to the natural order? Or is it just the way the toString works?
This is the natural order only.
|
 |
Muthaiah Ramanathan
Ranch Hand
Joined: May 16, 2005
Posts: 102
|
|
Oh Ok. Thanks Ankit. I wonder why sun didn't mention this clearly.
BTW - I did search for Priority queue before posting a new thread and didn't find these threads!
|
 |
 |
|
|
subject: Priority Queue Clarification
|
|
|