aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Priority Queue Ordering 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 "Priority Queue Ordering" Watch "Priority Queue Ordering" New topic
Author

Priority Queue Ordering

Babugouda patil
Ranch Hand

Joined: Mar 13, 2011
Posts: 35

Hi,

As per K&B book PriorityQueue is in sorted by order but I have question here when first I added elements into priorityqueue and printed it worked fine but after adding another element into priorityqueue order got changed and am not able to predict answer for this can anyone explain me.

LinkedList worked fine but PriorityQueue is not working





output :
LikedList Queue before:[1, 5, 4, 2]
LikedList Queue after:[1, 5, 4, 2, 0]
PriorityQueue Queue before:[1, 2, 4, 5]
PriorityQueue Queue after:[0, 1, 4, 5, 2]

Please explain me.



SCJP 1.6 91%
Take certification exam as a point of learning not just for certificate
Bhanu Prakash k
Greenhorn

Joined: Apr 18, 2011
Posts: 6
Babugouda,

This link might help you to clarify your question.

-Bhanu.

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

First, thanks to both of you for making me aware of this problem. .I changed the code to get proper output (after some errors in casting....etc). Please let me know if my code can be shorter/better :



Output :



thanks again !


SCJP 6. Learning more now.
Babugouda patil
Ranch Hand

Joined: Mar 13, 2011
Posts: 35


Thanks for this short coding Rhul Now i have question if I get this type question in exam what ans i should give According K & B book the PQ is sorted
please answer me
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Babugouda patil wrote:

Now i have question if I get this type question in exam what ans i should give According K & B book the PQ is sorted.

Ikpefua wrote:

Hello Babugouda, your question is interesting. Now let us assume you get a question that asks you if PriorityQueue is Sorted, your answer should be YES, this is because elements of a PriorityQueue MUST be mutually comparable meaning that the objects you add to the PriorityQueue MUST implement the Comparable interface. Thit is a FUNDAMENTAL characteristics of Sorted collections.

Lets assume that that you are asked the output of an iteration, your answer should be that it is NOT guaranteed. If you have any doubts you should go to the official java API http://download.oracle.com/javase/6/docs/api/ for more details. I already did that and this is what I discovered:

A.Does NOT permit insertion of non-comparable objects (doing so may result in ClassCastException). Answer to the first question.

B.The Iterator provided in method iterator() is NOT guaranteed to traverse the elements of the priority queue in any particular order. Answer to second question.

Thank you once again for your intelligent question! I have just learnt something new about PriorityQueues!.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Babugouda patil
Ranch Hand

Joined: Mar 13, 2011
Posts: 35


Hi All,

Thanks for the complement
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
Sorry... I misread my output.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello Everybody,
I also want to say in addition that in terms of iteration, HashSet behaves in a similar way as in PriorityQueue. It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. OUKC Oracle mock exams specifically has a question that tests your knowledge on the iteration order in HashSet, this means that you are likely to find a similar question in the real exams. So we now know the answer:

iteration order of HashSet and PriorityQueue are NOT guaranteed!.

Babugouda patil
Ranch Hand

Joined: Mar 13, 2011
Posts: 35


Yes Jacob I agree with you. If we get answer from the author it will be good and we can guarantee it.
 
wood burning stoves
 
subject: Priority Queue Ordering