File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Offer VS add

 
Sudhanshu Mishra
Ranch Hand
Posts: 235
1
Eclipse IDE Fedora Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a code which inserts the elements in the priority queue using offer and add method.I am just unable to figure out the order of sorting.
The code is

Now,why does 7 comes before 4 and 6 if the priority queue maintains the natural order.
Please guide me through...
Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Javadoc of its iterator() method, which is used internally when printing:
Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

This is also said in the class' main Javadoc:
he Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).
.
The priority is only guaranteed for poll(), peek(), remove() and element().
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic