• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorted output  RSS feed

 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is that despite using a comparator the output of the program is not sorted?



Output: [1, 3, 2]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raja,
That's because toString() doesn't print out in the order for the queue.

Try using this code to print the values in the queue:

 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya your code works fine. But the thing is how is the below code in which I printed the treeset and it comes in the correct order?


Output: [A, B, C]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because for TreeSet, it happens to print in that order. PriorityQueue isn't implemented that way.

I know that isn't a satisfactory answer, but it is just based on the how the classes happen to be implemented.
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Just for clarification, the priority queue when printed using System.out.println gives some output but internally it is sorted right in accordance to the lines of code you posted?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be that it isn't stored that way. Or it could be that the iteration logic doesn't look at it. This would depend on the implementation of the JDK you are using.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is partly documented in PriorityQueue's javadoc:
The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

Although PriorityQueue's own javadoc doesn't mention it, the javadoc of AbstractCollection's toString() method (which is inherited by PriorityQueue and many other collection implementations) says that toString() uses iterator():
The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]").
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

When exactly should I use priority queue?
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by going through the Java™ Tutorials. You will find a page about queues under the interfaces link and another under the implementations link. You will probably find more if you look up PriorityQueue.
The idea is that your priority queue automatically sorts itself according to some criterion you supply, those with the highest priority going to the head of the queue.The first category might include gunshot, severe chest pain, etc. The second bleeding profusely, the third fall onto one's head, the fourth broken arm, the fifth uncontrollable migraine, the last severe flu. The ambulance people will divert an ambulance from somebody with a broken arm if somebody phones and says they have sudden severe chest pain, because the chest pain might kill them within five minutes. These categories are pulled out of thin air/the back of my head, so don't look on them as scientifically accurate. Phone calls to the ambulance might be ordered in a priority queue, those with high priority moving first.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!