首页 > 其他 > 详细

TreeSet应用的例子

时间:2017-05-07 16:36:57      阅读:255      评论:0      收藏:0      [点我收藏+]
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应用的例子

原文:http://www.cnblogs.com/zyy1130/p/6821148.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!