• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Doubt on Priority Queue

 
Ranch Hand
Posts: 138
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I have try to run the below code,In that code the result is somewhat unpredictable.



Output:



According to which priority the elements are add in the priority queue?Please guide me.
 
Greenhorn
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I think this thread describe similar question:

https://coderanch.com/t/535172/java-programmer-SCJP/certification/Priority-Queue-Ordering

Very valuable note in this thread from Ikpefua:

1. PriorityQueue does NOT permit insertion of non-comparable objects (doing so may result in ClassCastException).
2. The Iterator provided in method iterator() is NOT guaranteed to traverse the elements of the PriorityQueue in any particular order.

If you want selecting or printing elements from PriorityQueue in order according their priority, use peek() or poll() method.
Printing PriorityQueue elements with Iterator or like in your example only using

does not guarantee ordering.
 
reply
    Bookmark Topic Watch Topic
  • New Topic