首页 > 代码库 > TreeSet应用的例子

TreeSet应用的例子

public class Student implements Comparable<Student>{
    int stuno;
String name;
int score;
public int getStuno() {
    return stuno;
}
public void setStuno(int stuno) {
    this.stuno = stuno;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getScore() {
    return score;
}
public void setScore(int score) {
    this.score = score;
}
public Student(int stuno, String name, int score) {
    super();
    this.stuno = stuno;
    this.name = name;
    this.score = score;
}
@Override
public String toString() {
    return "Student [stuno=" + stuno + ", name=" + name + ", score=" + score
            + "]";
}
@Override
public int compareTo(Student o) {
if(this.score-o.score==0){
    return this.stuno-o.stuno;
}
return this.score-o.score;
    
    
}

}
import java.util.TreeSet;

public class TestTree {
    public static void main(String[] args) {
    TreeSet<Student> set=new TreeSet<Student>();
 set.add(new Student(1, "james", 90));
 set.add(new Student(2, "go", 80));
 set.add(new Student(3, "jobs",65));
 set.add(new Student(4, "bill",65));
 set.add(new Student(5, "aoache",80));
 for(Student s:set){
     System.out.println(s);
 }

    }

}

 

TreeSet应用的例子