Hi
Not sure how the elements are stored in the Priority Queue but I have got something from
Java API Documentation.
This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).
So if you modify the code, you will get the elements in correct order.
Regards
Padma