• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

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.
 
Hug your destiny! And hug this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic