首页 > 编程语言 > 详细

Comparable<T> & Comparator<T>关于集合排序

时间:2021-07-27 20:43:38      阅读:30      评论:0      收藏:0      [点我收藏+]

Comparable<T>:

  • Comparable<T>:自然排序
  • 需要排序的类必须强制实现Comparable接口,并且实现其comparaTo(T t)方法
  • 目前只能使用TreeSet实现排序(jdk1.8)
 1 public class Student implements Comparable<Student>{
 2 
 3     private int sno;
 4     private String sname;
 5     private String sex;
 6     private String major;
 7     private Date birth;
 8     private double score;
 9 
10     public Student(int sno, String sname, String sex, String major, Date birth, double score) {
11         this.sno = sno;
12         this.sname = sname;
13         this.sex = sex;
14         this.major = major;
15         this.birth = birth;
16         this.score = score;
17     }
18 
19     /**
20      *   自然排序的比较方法
21      */
22     @Override
23     public int compareTo(Student s) {
24         return this.sname.compareTo(s.getSname());
25     }
26 
27     //setter/getter
28     //toString
29 
30 }
 1 //基于数组生成List集合
 2 List<Student> list = Arrays.asList(
 3     new Student(1002,"jack","男","挖掘机专修",new Date(),88),
 4     new Student(1005,"rose","女","挖掘机专修",new Date(),55),
 5     new Student(1006,"lily","男","挖掘机专修",new Date(),59),
 6     new Student(1001,"lucy","男","挖掘机专修",new Date(),44),
 7     new Student(1003,"jack","男","挖掘机专修",new Date(),39),
 8     new Student(1011,"tom","男","挖掘机专修",new Date(),59),
 9     new Student(1004,"bobo","男","挖掘机专修",new Date(),89),
10     new Student(1008,"aricle","男","挖掘机专修",new Date(),59),
11    
12 );
13 //将List集合转换为Set
14 Set<Student> set = new TreeSet<>(list);
15 for (Student s : set) {
16     System.out.println(s);
17 }

Comparator<T>:

  • Comparator<T>:排序比较器

  1.匿名内部类的写法

 1 List<Student> list = Arrays.asList(
 2     new Student(1002,"jack","男","挖掘机专修",new Date(),88),
 3     new Student(1005,"rose","女","挖掘机专修",new Date(),55),
 4     new Student(1006,"lily","男","挖掘机专修",new Date(),59),
 5     new Student(1001,"lucy","男","挖掘机专修",new Date(),44),
 6     new Student(1003,"jack","男","挖掘机专修",new Date(),39),
 7     new Student(1011,"tom","男","挖掘机专修",new Date(),59),
 8     new Student(1004,"bobo","男","挖掘机专修",new Date(),89),
 9     new Student(1008,"aricle","男","挖掘机专修",new Date(),59),
10     new Student(1012,"admin","男","挖掘机专修",new Date(),99),
11     new Student(1010,"softeem","男","挖掘机专修",new Date(),46),
12     new Student(1007,"ase","男","挖掘机专修",new Date(),32),
13     new Student(1009,"jojo","男","挖掘机专修",new Date(),10)
14 );
15 //排序(使用匿名内部类)
16 Collections.sort(list, new Comparator<Student>() {
17     @Override
18     public int compare(Student s1, Student s2) {
19         return s1.getSname().compareTo(s2.getSname());
20     }
21 });
22 
23 for (Student s : list) {
24     System.out.println(s);
25 }

  2.单独创建排序比较器

 1 public class StudentComparator implements Comparator<Student> {
 2     //用于排序的字段 sno,sname,score,birth
 3     private String field;
 4 
 5     public StudentComparator(String field) {
 6         this.field = field;
 7     }
 8 
 9     @Override
10     public int compare(Student s1, Student s2) {
11         switch (field){
12             case "sno":
13                 //按学号排序
14                 return s1.getSno() - s2.getSno();
15             case "sname":
16                 //按姓名排序
17                 return s1.getSname().compareTo(s2.getSname());
18             case "score":
19                 //按学分排序
20                 return (int)(s2.getScore() - s1.getScore());
21             case "birth":
22                 //按生日排序
23                 return s1.getBirth().compareTo(s2.getBirth());
24             default:
25                 return 0;
26         }
27     }
28 
29 }
1   //使用独立比较器
2         Collections.sort(list,new StudentComparator("score"));
3 
4         for (Student s : list) {
5             System.out.println(s);
6         }

 

Comparable<T> & Comparator<T>关于集合排序

原文:https://www.cnblogs.com/li-hui-feng001/p/15067577.html

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