[数据结构]TreeSet的条件筛选

要求排序:比较数语外成绩,如果相同则往后推进

java 复制代码
import java.util.TreeSet;

public class Main{
    public static void main(String[] args){
        //创建学生对象
        Student s1 = new Student("zhangsan",23,90,99,50);
        Student s2 = new Student("lisi",21,75,78,39);
        Student s3 = new Student("wangwu",26,23,53,21);
        Student s4 = new Student("zhaoliu",25,65,34,78);

        //创建集合
        //希望数据唯一的话用set 默认用hashset
        //希望用唯一又希望排序的话 就用Treeet
        TreeSet<Student> ts = new TreeSet<>();
        //3.添加元素
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        //这里要先指定排序规则
        System.out.println(ts);
    }
}
java 复制代码
public class Student implements Comparable<Student>{
    private String name;
    private int age;
    private int chinese;
    private int math;
    private int english;

    public Student() {
    }

    public Student(String name, int age, int chinese, int math, int english) {
        this.name = name;
        this.age = age;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return chinese
     */
    public int getChinese() {
        return chinese;
    }

    /**
     * 设置
     * @param chinese
     */
    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    /**
     * 获取
     * @return math
     */
    public int getMath() {
        return math;
    }

    /**
     * 设置
     * @param math
     */
    public void setMath(int math) {
        this.math = math;
    }

    /**
     * 获取
     * @return english
     */
    public int getEnglish() {
        return english;
    }

    /**
     * 设置
     * @param english
     */
    public void setEnglish(int english) {
        this.english = english;
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + ", chinese = " + chinese + ", math = " + math + ", english = " + english + "}";
    }

    @Override
    public int compareTo(Student o) {
        //比较两者的总分
        int sum1=this.getChinese()+this.getMath()+this.getEnglish();
        int sum2=o.getChinese()+o.getMath()+o.getEnglish();
        int i =sum1-sum2;
        i = i==0?this.getChinese()-o.getChinese():i;
        i = i==0?this.getMath()-o.getMath():i;
        i = i==0?this.getEnglish()-o.getEnglish():i;
        i = i==0?this.getAge()-o.getAge():i;
        i = i==0?this.getName().compareTo(o.getName()):i;
        return i;
    }
}
相关推荐
UFIT2 分钟前
NoSQL之redis哨兵
java·前端·算法
刘 大 望6 分钟前
数据库-联合查询(内连接外连接),子查询,合并查询
java·数据库·sql·mysql
怀旧,12 分钟前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵25 分钟前
有效的括号题解
数据结构·算法·
大春儿的试验田1 小时前
Parameter ‘XXX‘ not found. Available parameters are [list, param1]
java
Java 技术轻分享1 小时前
《树数据结构解析:核心概念、类型特性、应用场景及选择策略》
数据结构·算法·二叉树··都差速
我很好我还能学1 小时前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
程序员JerrySUN1 小时前
[特殊字符] 深入理解 Linux 内核进程管理:架构、核心函数与调度机制
java·linux·架构
2302_809798322 小时前
【JavaWeb】Docker项目部署
java·运维·后端·青少年编程·docker·容器
蓝婷儿2 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习