Порядок элементов в PriorityQueue
Создается PriorityQueue
из элементов Integer
с использованием компаратора
IntComparator intComparator = new IntComparator();
PriorityQueue<Integer> integerPriorityQueue2 = new PriorityQueue<>(intComparator);
integerPriorityQueue2.add(4);
integerPriorityQueue2.add(3);
integerPriorityQueue2.add(5);
integerPriorityQueue2.add(9);
integerPriorityQueue2.offer(1);
Компаратор:
@Override
public int compare(Integer o1, Integer o2) {
return (o1 - o2);
}
Проходим по полученной коллекции итератором:
System.out.print("IntegerPriorityQueue2. Using an iterator: ");
Iterator<Integer> iterator2 = integerPriorityQueue2.iterator();
while(iterator2.hasNext()) {
System.out.print(iterator2.next() + ", ");
}
System.out.println("");
Ожидание:
1, 3, 4, 5, 9
Реальность:
1, 3, 5, 9, 4
В чем причина?