• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Priority Queue Clarification

 
Muthaiah Ramanathan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Muthaiah Ramanathan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW - changing it to String also results in the same.

 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This problem has been asked multiple times. See this or this...
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic