码迷,mamicode.com
首页 > 其他好文 > 详细

TreeSet应用的例子

时间:2017-05-07 16:43:58      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:public   core   add   setname   getname   string   int   set   tree   

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应用的例子

标签:public   core   add   setname   getname   string   int   set   tree   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!