File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 9280
    
  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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Priority Queue Clarification
 
Similar Threads
Doubt in PriorityQueue
PriorityQueue order?
compare method arguments ?
Modifying PriorityQueue keys while in queue
Cannot understand how PriorityQueue gets ordered using a Comparator