package com.czie.iot1913.lps.itheima03;
import java.util.Comparator;
import java.util.TreeSet;
/**
* @author 1944900433@qq.com
* @date 2022-03-18 22:55
*/
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num=s2.getsum()-s1.getsum();
int num2=num==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});
Student s1 = new Student("刘品金",90,95);
Student s2 = new Student("刘品木",80,96);
Student s3 = new Student("刘品水",100,99);
Student s4 = new Student("刘品火",60,98);
Student s5 = new Student("刘品土",70,90);
Student s6 = new Student("刘品山",70,90);
Student s7 = new Student("刘品山",50,20);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
ts.add(s7);
for (Student s:ts){
System.out.println(s.getName()+","+s.getChinese()+","+s.getMath());
}
}
}
package com.czie.iot1913.lps.itheima03;
/**
* @author 1944900433@qq.com
* @date 2022-03-18 22:55
*/
public class Student {
private String name;
private int chinese;
private int math;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public Student(String name, int chinese, int math) {
this.name = name;
this.chinese = chinese;
this.math = math;
}
public Student() {
}
public int getsum(){
return this.math+this.chinese;
}
}