File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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???

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Adi Dinita
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • 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: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic