TreeSet学生成绩排序

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;
}
}
posted @   刘品水  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示