首页 > 其他分享 >自定义排序规则

自定义排序规则

时间:2024-09-06 19:52:01浏览次数:8  
标签:name 自定义 age ts Student 规则 new 排序 public

Comparator接口的使用:

TreeSet集合存储自定义类型的对象时,必须指定排序规则,支持如下两种方式来指定比较规则。

方式一

让自定义的类实现Comparable接口,重写里面的compareTo方法来指定比较规则。

​//属性类
public class Student implements Comparable<Student>{
    private String name;
    private Integer age;
​
    public Student() {
    }
​
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public Integer getAge() {
        return age;
    }
​
    public void setAge(Integer age) {
        this.age = age;
    }
​
    @Override
    public int compareTo(Student o) {
       int num=this.age-o.age;
     int num1=num==0?this.name.compareTo(o.name):num;
        return num1;
    }
}


​
​
import java.util.TreeSet;
​
public class TreeSetDemo1 {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<Student>();
        Student stu1 = new Student("张三",18);
        Student stu2 = new Student("李四",16);
        Student stu3 = new Student("王五",19);
        Student stu4 = new Student("赵二",21);
        Student stu5 = new Student("孙达",16);
        Student stu6 = new Student("赵二",21);
        ts.add(stu2);
        ts.add(stu3);
        ts.add(stu4);
        ts.add(stu5);
        ts.add(stu6);
​
        //遍历
        for(Student s:ts){
            System.out.println(s.getName()+" ,"+s.getAge());
        }
    }
}

方式二

通过调用TreeSet集合有参数构造器,可以设置Comparator对象(比较器对象,用于指定比较规则)

​
//属性类
public class Student {
    private String name;
    private Integer age;
​
    public Student() {
    }
​
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public Integer getAge() {
        return age;
    }
​
    public void setAge(Integer age) {
        this.age = age;
    }
​
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
import java.util.Comparator;
import java.util.TreeSet;
​
public class TreeSetDemo2 {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num=s1.getAge()-s2.getAge();
                int num2=num==0?s1.getName().compareTo(s2.getName()):num;
                return num2;
            }
        });
        Student stu1 = new Student("张三",18);
        Student stu2 = new Student("李四",16);
        Student stu3 = new Student("王五",19);
        Student stu4 = new Student("赵二",21);
        Student stu5 = new Student("孙达",16);
        Student stu6 = new Student("赵二",21);
        ts.add(stu1);
        ts.add(stu2);
        ts.add(stu3);
        ts.add(stu4);
        ts.add(stu5);
        ts.add(stu6);
​
        //遍历
        for(Student s:ts){
            System.out.println(s.getName()+" ,"+s.getAge());
        }
    }
}

标签:name,自定义,age,ts,Student,规则,new,排序,public
From: https://blog.csdn.net/2401_85045690/article/details/141967973

相关文章

  • VUE框架Vue3使用自定义事件的方式传递数据------VUE框架
    <template><!--给User绑定事件--><!--带参数的方法不要有括号,否则识别不到,我不懂原理...--><User@event1="showInfo"></User></template><script>importUserfrom"./components/User.vue"exportdefault{......
  • C语言-第六章-加餐:其他自定义类型
    传送门:C语言-第六章:结构体目录第一节:位段    1-1.位段是什么    1-2.位段的大小第二节:联合体    2-1.联合体是什么    2-2联合体的大小第三节:枚举类型    3-1.枚举是什么第四节:结构体中的柔性数组    4-1.柔性数组......
  • 功能发布-自定义SQL查询
    引言本期主要为大家介绍ClkLog九月上线的新功能-自定义SQL查询。什么是自定义SQL查询?自定义SQL查询是指根据具体的应用场景和需求,由开发者或数据库管理员自行编写的SQL(StructuredQueryLanguage,结构化查询语言)语句。自定义SQL查询有什么作用?以精确的方式从数据库中检索数据。在Cl......
  • vue自定义指令
    现象:可使用自定义指令v-per实现:index.js文件importperfrom"./per"importper2from"./per2"//批量注册指令(现在就一个permission)constdirectives={per,per2}//注册的一般写法,循环遍历directives,通过vue.directive注册exportdefault{inst......
  • 使用itertools 中的groupby 对字典数组进行分组后排序
    有个需求需要对数据进行分组然后对分组后的组内数据继续排序fromitertoolsimportgroupby#示例数据data=[{'a':"攻击队1",'b':'张三','c':3},{'a':"攻击队1",'b':'张三1','c':1},......
  • HUAWEI HiCar新验收标准执行规则
    HUAWEIHiCar是华为推出的一项创新技术,旨在将手机与汽车深度融合,通过强大的多设备互联能力,将手机的应用和服务无缝延伸至汽车,为用户打造以手机为核心的全场景智慧出行体验。该技术不仅提升了驾驶过程中的安全性和便捷性,还为用户带来了更加丰富、智能的娱乐和信息服务。针对HUAWEIH......
  • CI3在路由规则中使用回调函数来处理逆向引用
      创建yy类classyyextendsCI_Controller{publicfunction__construct(){parent::__construct();}publicfunctionfy(){echo'home/yy';}publicfunctionfy2(){echo'home/yy2';}} $route[......
  • LEAN 类型理论之注解(Annotations of LEAN Type Theory)—— 归纳类型(Inductive Type)的
            使用规则(EliminationRule),也叫解构规则(DestructionRule),与构建规则(IntroductionRule),也叫建构规则(ConstructionRule),相对应。        即,构建规则(IntroductionRule),定义了该类型的正规元素是如何构建出来的;而,使用规则(EliminationRule),定义了该类型的正......
  • 【数据结构】快速排序与归并排序的非递归实现
    ......
  • 快速排序(动图详解)(C语言数据结构)
    快速排序:        快速排序是Hoare于1962年提出的一种二叉树结构的交换排序方法,其基本思想为:        任取待排序元素序列中的某元素作为基准值,按照该排序码将待排序集合分割成两子序列,左子序列中所有元素均小于基准值,右子序列中所有元素均大于基准值,然后最左......