File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Priority queue" Watch "Priority queue" New topic

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 !

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

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
I agree. Here's the link:
subject: Priority queue
It's not a secret anymore!