/**
* 学生类
* @author Administrator
*
*/
public class Student { private String sno ;
private String sname ;
private Integer score ; public Student(String sno, String sname, Integer score) {
super();
this.sno = sno;
this.sname = sname;
this.score = score;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
} import java.util.Comparator;
/**
* 比较算法的类,比较器
* @author Administrator
*
*/
public class MyCmp implements Comparator<Student>{ @Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if (o1.getScore() > o2.getScore()) {
return 1;
} else if (o1.getScore() < o2.getScore()) { return -1;
        } else {
return 0;
}
} package tr
import java.util.TreeSet;
//测试类
public class Test { public static void main(String[] args) {
TreeSet<Student> set = new TreeSet<Student>(new MyCmp());
Student s1 = new Student("1001","chen",67);
Student s2 = new Student("1001","zhang",17);
Student s4 = new Student("1003","zeng",47);
Student s5 = new Student("1004","wang",87);
Student s6 = new Student("1005","chao",67);
Student s3 = new Student("1002","zhou",62);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
set.add(s6);for(java.util.Iterator<Student> it= set.iterator();it.hasNext();){
Student s = it.next();
System.out.println(s.getSno()+","+s.getSname()+","+s.getScore());
}
} }

/**
* 学生类
* @author Administrator
*
*/
public class Student1 implements Comparable<Student1>{ private String sno ;
private String sname ;
private Integer score ; public Student1(String sno, String sname, Integer score) {
super();
this.sno = sno;
this.sname = sname;
this.score = score;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
} // 自定义排序规则
@Override
public int compareTo(Student1 s) {
if (this.getScore() > s.getScore()) {
return 1;
} else if (this.getScore() < s.getScore()) {
return -1;
} else {
return 0;
}
} }

最新文章

  1. java.io.IOException: invalid header field
  2. 《CoffeeScript应用开发》学习: CoffeeScript高级用法
  3. .map文件的作用以及在chorme下会报错找不到jquery-1.10.2.min.map文件,404 的原因
  4. 记录远程桌面登录者的IP和MAC
  5. Oracle 如何对时间进行简单加减运算
  6. Sql Server聚集索引创建
  7. 关于ligerui和其他前端脚本的学习方法(适用于自己)
  8. VIPS: a VIsion based Page Segmentation Algorithm
  9. javascript: 常用操作
  10. 用Socket编写的聊天小程序
  11. render函数(转)
  12. scrapy基本使用(一)
  13. SpringBoot java.lang.IllegalArgumentException: Request header is too large
  14. lambda表达式,变量作用域
  15. html学习笔记(一)
  16. spring boot之使用通用Mapper批量添加数据
  17. log4net 存储到oracle 调试 Could not load type [log4net.Appender.OracleAppender]
  18. 【BZOJ】4147: [AMPPZ2014]Euclidean Nim
  19. (译)Getting Started——1.2.3 Defining the Interaction(定义交互)
  20. LA 3126 出租车

热门文章

  1. timestamp时间格式
  2. ctrl+c以及写操作失败和flush
  3. 解题报告 『机器翻译(vector)』
  4. 解决win7无法运行bat批处理文件的方法
  5. BUAA-OO第二单元小结
  6. 【Jest】笔记二:Matchers匹配器
  7. 芯灵思Sinlinx A33开发板boa与CGI移植
  8. 多线程之BlockingQueue中 take、offer、put、add的一些比较
  9. Windows和Office激活工具Kmsauto Net
  10. 【转】Syncthing的安装与使用