• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on PriorityQueue?

 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic