File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in add() and offer() method of PriorityQueue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in add() and offer() method of PriorityQueue" Watch "Doubt in add() and offer() method of PriorityQueue" New topic
Author

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

Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

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?


Dhruva
Adi Dinita
Greenhorn

Joined: Oct 21, 2010
Posts: 5

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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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
 
subject: Doubt in add() and offer() method of PriorityQueue