Comparator
이 정확한지 확인하십시오. 문제는 귀하가 Iterator
을 사용하여 목록을 훑어 본다는 것입니다. PriorityQueue
documentation 상태 :
반복자 방법 반복자 제공()는 임의의 특정 순서로 우선 순위 큐의 요소 이송 보장되지 않는다.이처럼 PriorityQueue
을 반복한다면
, 당신은 올바른 결과를 볼 수 :
while (!pq.isEmpty())
System.out.println(pq.poll().getName());
}
나는 완전히 입증이 답변의 끝에 예를 포함 시켰습니다.
PriorityQueue
을 삭제하고 싶지 않은 경우 몇 가지 조치를 취할 수 있습니다. 개인적으로 나는이 방법을 사용하지 않는 편이 바람직하기 때문에 PriorityQueue
의 초기 선택이 유스 케이스에 맞지 않기 때문에이 방법을 사용하지 않는 것이 좋습니다. 다음 다시 추가,
Student[] students = pq.toArray(new Student[pq.size()]);
Arrays.sort(students, new StComp());
for (Student s : students) {
System.out.println(s.getName() + " " + s.getCgpa() + " " + s.getId());
}
또는 폴링 동안 Collection
의 일종에 추가 :
당신은 정렬 된 배열을 반복, 당신의 Comparator
구현을 사용하여 예를 들어 그들을 분류, 배열로 PriorityQueue
를 복사 할 수 PriorityQueue
에, 예를 들면 :
Collection<Student> temp = new LinkedList<>();
while (!pq.isEmpty()) {
Student s = pq.poll();
System.out.println(s.getName() + " " + s.getCgpa() + " " + s.getId());
temp.add(s);
}
pq.addAll(temp);
이 예는 데이터를 사용하여 설명하기 :,992,967,
public class Main {
public static void main(String[] args) {
PriorityQueue<Student> pq = new PriorityQueue<>(new StComp());
pq.add(new Student("John", 75, 50)); // Student name, grade average, id
pq.add(new Student("Mark", 8, 24));
pq.add(new Student("Shafaet", 7, 35));
pq.poll();
pq.poll();
pq.add(new Student("Samiha", 85, 36));
pq.poll();
pq.add(new Student("Ashley", 9, 42));
pq.add(new Student("Maria", 6, 46));
pq.add(new Student("Anik", 95, 49));
pq.add(new Student("Dan", 95, 50));
pq.poll();
// Not guaranteed to be in priorty order
System.out.println("Using PriorityQueue's Iterator, may not be in the correct priority order.");
for (Student s : pq) {
System.out.println(s.getName() + " " + s.getCgpa() + " " + s.getId());
}
// Correct order, but removes from the Priority Queue
System.out.println("\nIterating until empty using PriorityQueue.poll(), will be in the correct order.");
while (!pq.isEmpty()) {
Student s = pq.poll();
System.out.println(s.getName() + " " + s.getCgpa() + " " + s.getId());
}
}
}
학생 홈페이지
public class Student {
private double cgpa;
private String name;
private int id;
public Student(String name, double cgpa, int id) {
this.name = name;
this.cgpa = cgpa;
this.id = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public double getCgpa() {
return cgpa;
}
}
StComp (질문에서 변경되지 않은 논리)
public class StComp implements Comparator<Student> {
@Override
public int compare(Student st1, Student st2) {
if (st1.getCgpa() == st2.getCgpa()) {
if (st1.getName().equals(st2.getName())) {
return st1.getId() - st2.getId();
} else {
return st1.getName().compareTo(st2.getName());
}
} else {
return (st1.getCgpa() < st2.getCgpa()) ? 1 : -1;
}
}
}
(이름, 단수이어야 함)
출력 (나를 적어도, 결과는 첫째
Iterator
변형에 따라 다를 수 있습니다에 대한)
Using PriorityQueue's Iterator, may not be in the correct priority order.
Dan 95.0 50
Ashley 9.0 42
Maria 6.0 46
Shafaet 7.0 35
Iterating until empty using PriorityQueue.poll(), will be in the correct order.
Dan 95.0 50
Ashley 9.0 42
Shafaet 7.0 35
Maria 6.0 46
'st1.getCgpa()'유형을 반환합니까? 'Double','double','BigDecimal'? –
int id; 문자열 이름; 이중 cgpa; – Kokufuu
'st1.getCgpa() == st2.getCgpa()'는'double'과 크게 다르지 않습니다. cgpa 대신에'BigDecimal'을 사용해보십시오 –