• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in add() and offer() method of PriorityQueue

 
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In PriorityQueue,if we add anything to it than PriorityQueue forms a queue in natural order.



when i execute this code,it produces output : [p, s]
output shows that it store in natural order



when i add l to it than execute it,output: [l, s, p]
it should be in natural order but it is not.why??

when i add a to it than produces the output:[a,l,p,s]
which is in natural order.why?

if i add w instead of a in code like

than its output is:[l, s, p, w]
and now again the output is not sorted.why??
and the offer method works same as the add method if i replace add with offer and it produces same output..

I do not understand how jvm works with these methods??and what is the difference between add and offer method???

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you display a PriorityQueue directly or using iterator, the ordering of elements is not guaranteed. The natural ordering is only guaranteed for peek and offer methods. Checkout the javadocs for PriorityQueue class, you'll get your answer...
 
Ranch Hand
Posts: 67
Mac Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:When you display a PriorityQueue directly or using iterator, the ordering of elements is not guaranteed. The natural ordering is only guaranteed for peek and offer methods. Checkout the javadocs for PriorityQueue class, you'll get your answer...



Hello Ankit,

I tried this :


that resulted in [l,s,p] instead [l,p,s]

I am also confused with usage of offer() for NaturalOrder
Can you help me, please?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the offer()/add() methods only insert the element into the queue. If you want a predictable order you should use peek/poll which return the head of the queue.

For example:
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Myself wrote:The natural ordering is only guaranteed for peek and offer methods.


I wrote the method name wrong, it should be peek and poll not offer. Anyway, you can search the forum for "PriorityQueue toString", this doubt was asked many times. The toString method of PriorityQueue doesn't return elements in sorted order...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic