ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JavaSE-15.2.4【案例-成绩排序(比较器排序Comparator实现、自然排序Comparable实现)】

2021-06-01 13:34:06  阅读:290  来源: 互联网

标签:15.2 chinese Comparator int ts Student 排序 public math


 1 package day6.lesson2.anli1;
 2 
 3 import java.util.Comparator;
 4 import java.util.TreeSet;
 5 
 6 /*
 7 2.4 案例-成绩排序
 8 
 9     用TreeSet集合存储多个学生信息(姓名,语文成绩,数学成绩),并遍历该集合
10     要求:按照总分从高到低出现
11 
12     通过 比较器排序Comparator 实现
13     通过 自然排序Comparable 实现
14  */
15 //通过 比较器排序Comparator 实现
16 public class TreeSetDemo4 {
17     public static void main(String[] args) {
18         TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
19             @Override
20             public int compare(Student s1, Student s2) {
21 //                return 0;
22 
23                 /*int score_s1 = s1.getChinese() + s1.getMath();
24                 int score_s2 = s2.getChinese() + s2.getMath();
25                 int num = score_s2 - score_s1;
26                 return num;*/
27 
28                 int num = s2.getSum() - s1.getSum();
29                 int num2 = ( num==0 ? s1.getChinese()-s2.getChinese() : num );
30                 int num3 = ( num2==0 ? s1.getName().compareTo(s2.getName()) : num2 );
31                 return num3;
32             }
33         });
34 
35         Student s1 = new Student("tom", 100, 80);
36         Student s2 = new Student("amy", 85, 90);
37         Student s3 = new Student("sam", 79, 100);
38         Student s4 = new Student("jji", 90, 95);
39         Student s5 = new Student("bba", 95, 90);
40         Student s6 = new Student("cca", 95, 90);
41 
42         ts.add(s1);
43         ts.add(s2);
44         ts.add(s3);
45         ts.add(s4);
46         ts.add(s5);
47         ts.add(s6);
48 
49         for (Student s: ts){
50 //            System.out.println(s.getName() + "," + (s.getChinese()+s.getMath()) );
51             System.out.println(s.getName() + "," + s.getSum());
52         }
53         /*
54         jji,185
55         bba,185
56         cca,185
57         tom,180
58         sam,179
59         amy,175
60          */
61     }
62 }
 1 package day6.lesson2.anli1;
 2 
 3 public class Student {
 4 
 5     private String name;
 6     private int chinese;
 7     private int math;
 8 
 9     public Student() {
10     }
11 
12     public Student(String name, int chinese, int math) {
13         this.name = name;
14         this.chinese = chinese;
15         this.math = math;
16     }
17 
18     public void setName(String name) {
19         this.name = name;
20     }
21 
22     public void setChinese(int chinese) {
23         this.chinese = chinese;
24     }
25 
26     public void setMath(int math) {
27         this.math = math;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public int getChinese() {
35         return chinese;
36     }
37 
38     public int getMath() {
39         return math;
40     }
41 
42     public int getSum(){
43         return this.chinese + this.math;
44     }
45 }
 1 package day6.lesson2.anli1_1;
 2 
 3 import java.util.TreeSet;
 4 
 5 //通过 自然排序Comparable 实现
 6 public class TreeSetDemo5 {
 7     public static void main(String[] args) {
 8         TreeSet<Student> ts = new TreeSet<>();
 9 
10         Student s1 = new Student("tom", 100, 80);
11         Student s2 = new Student("amy", 85, 90);
12         Student s3 = new Student("sam", 79, 100);
13         Student s4 = new Student("jji", 90, 95);
14         Student s5 = new Student("bba", 95, 90);
15         Student s6 = new Student("cca", 95, 90);
16 
17         ts.add(s1);
18         ts.add(s2);
19         ts.add(s3);
20         ts.add(s4);
21         ts.add(s5);
22         ts.add(s6);
23 
24         for (Student s: ts){
25             System.out.println(s.getName() + "," + s.getSum());
26         }
27         /*
28         jji,185
29         bba,185
30         cca,185
31         tom,180
32         sam,179
33         amy,175
34          */
35     }
36 }
 1 package day6.lesson2.anli1_1;
 2 
 3 public class Student implements Comparable<Student>{
 4 
 5     private String name;
 6     private int chinese;
 7     private int math;
 8 
 9     public Student() {
10     }
11 
12     public Student(String name, int chinese, int math) {
13         this.name = name;
14         this.chinese = chinese;
15         this.math = math;
16     }
17 
18     public void setName(String name) {
19         this.name = name;
20     }
21 
22     public void setChinese(int chinese) {
23         this.chinese = chinese;
24     }
25 
26     public void setMath(int math) {
27         this.math = math;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public int getChinese() {
35         return chinese;
36     }
37 
38     public int getMath() {
39         return math;
40     }
41 
42     public int getSum(){
43         return this.chinese + this.math;
44     }
45 
46     @Override
47     public int compareTo(Student s) {
48 //        return 0;
49 
50         int num = s.getSum() - this.getSum();
51         int num2 = ( num==0 ? this.getChinese()-s.getChinese() : num);
52         int num3 = ( num2==0 ? this.getName().compareTo(s.getName()) : num2 );
53         return num3;
54     }
55 }

 

标签:15.2,chinese,Comparator,int,ts,Student,排序,public,math
来源: https://www.cnblogs.com/yppah/p/14836800.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有