• Post Reply Bookmark Topic Watch Topic
  • New Topic

if PriorityQueue object is initially sorted, why my output is not sorted ?  RSS feed

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output :
Abcee
Arrr
Cde
Bcc
Bff
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javadoc wrote: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()).
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
javadoc wrote: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()).


so why its called "priority queue" if the iterator providedis not promise that all the elemnts will sorted?
this happened only in priority queue, with TreeSet its works well
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan D'amico wrote:so why its called "priority queue" if the iterator providedis not promise that all the elemnts will sorted?

They will be sorted if you use it as the javadoc suggests. Have you read it?
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
Dan D'amico wrote:so why its called "priority queue" if the iterator providedis not promise that all the elemnts will sorted?

They will be sorted if you use it as the javadoc suggests. Have you read it?


yes . thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!