Hi, I think this
thread describe similar question:
https://coderanch.com/t/535172/java-programmer-SCJP/certification/Priority-Queue-Ordering
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() or
poll() method.
Printing PriorityQueue elements with Iterator or like in your example only using
does not guarantee ordering.