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