This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on PriorityQueue? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Doubt on PriorityQueue?" Watch "Doubt on PriorityQueue?" New topic
Author

Doubt on PriorityQueue?

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 179

Can anyone explain me "PriorityQueue orders its elements using user-defined priority .The priority can be simple as natural ordering".

--Here what does it mean user-defined priority

--And when I run my program it produces:-
[1, 3, 6, 8, 4, 7]
AND
[1, 3, 6, 8, 4, 7] //with the use of comparator

So that I think this is not any kind of ordering.please explain me

Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
Interesting question Ashok, I thought this was strange too. I looked up the PriorityQueue API and this is what I found there:


Maybe that's the reason why printing 'q' or iterating through the queue doesn't display the order.

Having said that, I tried to poll the queue and it displays the correct order. Maybe polling is the way to go

Those were my 2 cents, any thoughts people?
Seema
[ October 07, 2008: Message edited by: Seema Gaurav ]
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

You are right Seema. When you use an PriorityQue object in System.out.println(), then the toString method of AbstractCollection class is executed.

The toString implementation in AbstractCollection class iterates through a collection and displays the output as [element1, element2,...]

But when you use poll() or peek(), then the elements are traversed in sorted order...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Doubt on PriorityQueue?
 
Similar Threads
Priority queue
Queue
Confusion with Generics
comparator-someone answer this
Doubt in PriorityQueue