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 }