어리석은 것처럼 들릴지 모르지만, (키, 값) 쌍의 객체가 있고 키에 따라 정렬 할 때 의미가 있습니다. 내 요점을 설명하기 위해 :Java의 PriorityQueue가 중복 항목을 정렬하는 방법은 무엇입니까?
public class Pair implements Comparable<Pair> {
private int value;
private int key;
public Pair(int key, int value) {
this.key = key;
this.value = value;
}
@Override
public int compareTo(Pair o) {
if (this.key > o.key)
return 1;
else if (this.key < o.key)
return -1;
return 0;
}
}
public class program {
public static void main(String[] args) {
PriorityQueue<Pair> queue = new PriorityQueue<Pair>;
queue.add(new Pair(1,1));
queue.add(new Pair(1,2));
queue.add(new Pair(1,3));
Pair pair = queue.poll(); // What would be in pair?
}
}
무엇이 pair
에 있을까요? 첫 번째 또는 마지막으로 추가 된 요소? 아니면 결정할 가능성이없는 그들 중 누구입니까?
+1. –
그래서 제대로 이해한다면 - 내가 먼저 얻는 가치가 무엇인지에 의존 할 수는 없습니까? 출력에서 실제로 "FIFO"동작을하는 것처럼 보입니다. – Petr
API에 따르면 할 수는 없지만 내 테스트에서는 동일한 Pair.key에 대해 FIFO와 비슷한 동작이 표시됩니다. –