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

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

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);
    }
}

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;
    }
}

相关推荐

  1. [数据结构]TreeSet条件筛选

    2023-12-11 17:06:01       57 阅读
  2. 解决方案:Pandas如何多条件筛选数据

    2023-12-11 17:06:01       41 阅读
  3. python筛选数据库中表数量

    2023-12-11 17:06:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 17:06:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 17:06:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 17:06:01       82 阅读
  4. Python语言-面向对象

    2023-12-11 17:06:01       91 阅读

热门阅读

  1. 算法基础十一

    2023-12-11 17:06:01       43 阅读
  2. 十二.镜头知识之镜头分辨率(解析力)

    2023-12-11 17:06:01       52 阅读
  3. ElasticSearch

    2023-12-11 17:06:01       59 阅读
  4. Python——传参

    2023-12-11 17:06:01       55 阅读
  5. 算法训练营Day8(字符串)

    2023-12-11 17:06:01       54 阅读
  6. Python 语言在 Web 开发上有哪些应用框架?

    2023-12-11 17:06:01       56 阅读
  7. rk3288 android以太网和wifi共存

    2023-12-11 17:06:01       53 阅读
  8. DCNv2安装适配pytorch各个版本

    2023-12-11 17:06:01       59 阅读
  9. 【数据结构】栈(代码篇)

    2023-12-11 17:06:01       55 阅读
  10. python输出菱形字符图案 附实战代码

    2023-12-11 17:06:01       59 阅读