This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Priority queue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Priority queue" Watch "Priority queue" New topic
Author

Priority queue

amarkirt saroay
Ranch Hand

Joined: Mar 16, 2008
Posts: 167
Can anyone explain me the output for priority queue?
I am not able get why the first output gives result in sorted order? It should display numbers in insertion order(1,5,3,7,6,9,8).

Please explain !




SCJP-75%
SCWCD-82%
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

Please refer java doc for priority queue.

"Priority 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".
[ July 22, 2008: Message edited by: ramesh maredu ]

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Hello,

Priority Queue when we not use a Comparator uses the natural order, by natuaral order it means the Comparable the class implements, so Integer implements his comparable, and it is:



So the result is ok, take a look on this commented code:


I hope it helps.

Kind Regards,
Raphael Rabadan


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Priority queue