I was working on the Huffman. But I found the sorting algorithm of PriorityQueue problematic; it does not make enough compares! Then I just wrote a simple class to test the Collections's sorting and PriorityQueue's sorting:
public class Student implements Comparable<Student>{
String name;
int score;
int math;
public Student(int score, int math, String name) {
this.name = name;
this.score = score;
this.math = math;
}
public int compareTo(Student other) {
if (this.score > other.score) {
return -1;
} else if (this.score < other.score) {
return 1;
} else {
if (this.math > other.math) {
return -1;
} else {
return 1;
}
}
return 0;
}
public String toString() {
return("[" + name + " has Score: " + score + "(Math: " + math + ")]");
}
}
But I got the result like this(on the console):
Priority Queue::::::::::
[Jeremy Lin has Score: 2350(Math: 800)]
[Qian has Score: 2150(Math: 800)]
[PoorMath has Score: 2060(Math: 600)]
[Hui has Score: 1800(Math: 690)]
[Kaiyu has Score: 2060(Math: 800)]
[Chao has Score: 0(Math: 0)]
ArrayList sorted::::::::
[Jeremy Lin has Score: 2350(Math: 800)]
[Qian has Score: 2150(Math: 800)]
[Kaiyu has Score: 2060(Math: 800)]
[PoorMath has Score: 2060(Math: 600)]
[Hui has Score: 1800(Math: 690)]
[Chao has Score: 0(Math: 0)]
How to explain this? It's so wierd!
Thank you so much!!