Your comparison is the wrong way around.
PriorityQueue
puts the smallest item (based on your comparison) first, and, by saying return o2.compareTo(o1);
, you'll treat the largest integer as the smallest, but you need the smallest element needs to be first, not the largest.
You need to change that to:
return o1.compareTo(o2);
(note that I swapped the o1
and o2
)
Or, for what it's worth, you can remove the second argument completely from the constructor, as it will then use Integer.compareTo
, which will do what you want.