Hi, I think this thread
describe similar question:
Very valuable note in this thread from Ikpefua:
1. PriorityQueue does NOT permit insertion of non-comparable objects (doing so may result in ClassCastException).
2. The Iterator provided in method iterator() is NOT guaranteed to traverse the elements of the PriorityQueue in any particular order.
If you want selecting or printing elements from PriorityQueue in order according their priority, use peek()
Printing PriorityQueue elements with Iterator or like in your example only using
does not guarantee ordering.