私は、キーと値を持つPairという名前のあらかじめ定義されたクラスを持っています。私は、各ペアの自然な順序に基づいてPriorityQueueにそれらを格納します。ペアの1つの値を変更してからデキューすると、私が期待していたことは起こりませんでした。テストコードは以下の通りです。助けてください。私は混乱しているように感じる!投票を行うとPriorityQueueはどのように機能するのですか?
import java.util.*;
public class Test {
static class Pair {
int key;
int value;
Pair (int key, int value) {
this.key = key;
this.value = value;
}
}
public static void main(String[] args) {
PriorityQueue<Pair> pq = new PriorityQueue<Pair>(3, new Comparator<Pair>() {
@Override
public int compare(Pair p1, Pair p2) {
return p1.value - p2.value;
}
});
Pair p1 = new Pair(1, 31);
Pair p2 = new Pair(2, 32);
Pair p3 = new Pair(3, 33);
pq.offer(p1);
pq.offer(p2);
pq.offer(p3);
p2.value = 31;
p1.value = 32;
Pair p0 = pq.poll(); // It shows the reference p0 is p1 not expected p2.
// And what remain in pq are p2 with 31 and p3 with 33
}
}
ポーリング時にPriorityQueueがアイテムをソートすることがわかりました。私の例ではPriorityQueueが機能しなかったようです。
なぜp2が必要ですか? –
"ポーリング時にPriorityQueueがアイテムをソートすることはわかっています" < - 本当ですか? JavaDocは、pollメソッドのようなものは何も言わず、ポーリングメソッドは単純に、現在のオブジェクトをインデックス0に戻してから、そのキューをソートします。 –
@SotiriosDelimanolis私は、p2の 'value'(比較関数に使用される)が3つの' Pair'の中で最小であり、 'PriorityQueue'が' Pair'を最小値 'p2 'でポーリングすべきだと考えました。 –