Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in offer() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in offer()" Watch "doubt in offer()" New topic
Author

doubt in offer()

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hai,

I have a doubt in offer().
is this method add elements to the PriorityQueue in an order?
for eg :


Thanks
samura
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
No, you're wrong.

First:
add() and offer() in a priority queue do the same.

The priority queue is ordered, but not from the sequence, you put the elements in. It is ordered by natural order of the elements.

Trap:
if you just iterate the queue, the output might be the same as the sequence of input.
The order can be seen only if you poll the elements out individually.


Second
If you want to retain the order of your array, you shouldn't use a priority queue at all. Take a linked list.


Example (change offer to add and retry):



Try it.


Yours,
Bu.


all events occur in real time
 
wood burning stoves
 
subject: doubt in offer()
 
Similar Threads
priority queue doubt
doubt priorityqueue
PriorityQueue doubt
Priority queue
Priority Queue