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