This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Priority Queue Clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Priority Queue Clarification" Watch "Priority Queue Clarification" New topic
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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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
 
Similar Threads
Cannot understand how PriorityQueue gets ordered using a Comparator
Doubt in PriorityQueue
compare method arguments ?
Modifying PriorityQueue keys while in queue
PriorityQueue order?