• Java基础进阶集合-Comparable接口,Comparator比较器案例


    定义一个Employee类

    该类包括:

    • private 成员变量 name ,age,birthday,其中birthday为MyDate类的对象

    • 并为每一个属性定义getter setter方

    定义Mydate类包含:

    • private 成员变量 year,month,day 并为每一个属性定义getter setter方法

    创建该Employee类的5个对象,并把这些对象放入TreeSet集合中,分别按以下两种方式对集合中的元素进行排序,并遍历输出

    (1)使Employee实现Comparable接口,并按name排序,字典表顺序

    (2)创建TreeSet时传入Comparator对象,按生日日期的对员工进行先后顺序排序

    1992 - 05 - 12

    1992 - 05 - 18

    1992 - 07 - 15

    1993 - 06 - 15

    1998 - 02 - 03

    ①Emoplyee.java

    public class Employee implements Comparable<Employee>{
    
        private String name;;
        private int age;
        private MyDate birthday;
    
        public Employee(){}
    
        public Employee(int age){
            this.age = age;
        }
        public Employee(String name){this.name = name;}
    
        public Employee(String name, int age, MyDate birthday) {
            this.name = name;
            this.age = age;
            this.birthday = birthday;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public MyDate getBirthday() {
            return birthday;
        }
    
        public void setBirthday(MyDate birthday) {
            this.birthday = birthday;
        }
    
    
        @Override
        public int compareTo(Employee e) {
    
            if(this.getAge() == e.getAge()) {
                return this.getName().compareTo(e.getName());
            }else{
                return this.getAge() - e.getAge();
            }
        }
    
        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", birthday=" + birthday +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    ②MyDate.java

    public class MyDate {
        private int year;
        private int month;
        private int day;
    
        public MyDate(){
    
        }
    
        public MyDate(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    
        public int getYear() {
            return year;
        }
    
        public void setYear(int year) {
            this.year = year;
        }
    
        public int getMonth() {
            return month;
        }
    
        public void setMonth(int month) {
            this.month = month;
        }
    
        public int getDay() {
            return day;
        }
    
        public void setDay(int day) {
            this.day = day;
        }
    
        @Override
        public String toString() {
            return "MyDate{" +
                    "year=" + year +
                    ", month=" + month +
                    ", day=" + day +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    ③Test.java

    public class Test {
        public static void main(String[] args) {
    
            //创建TreeSet集合对象
            TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {//比较器比较生日
                @Override
                public int compare(Employee o1, Employee o2) {
    
                    int year = o1.getBirthday().getYear();
                    int month = o1.getBirthday().getMonth();
                    int day = o1.getBirthday().getDay();
    
                    int year1 = o2.getBirthday().getYear();
                    int month1 = o2.getBirthday().getMonth();
                    int day1 = o2.getBirthday().getDay();
    
                    int result = year - year1;
                    int result2 = result == 0  ? month - month1 : result ;
                    int result3 = result2 ==0 ? day - day1 : result2;
                    return result3;
                }
            });
    
            //创建Empployee对象
            Employee e1 = new Employee("zhangsan",20,new MyDate(1992,05,12));
            Employee e2 = new Employee("lisi",18,new MyDate(1992,05,18));
            Employee e3 = new Employee("wangwu",18,new MyDate(1992,07,15));
            Employee e4 = new Employee("zhaoliu",13,new MyDate(1992,05,12));
            Employee e5 = new Employee("xuqi",80,new MyDate(1993,06,15));
    
            //把Employee对象放到TreeSet集合中
            set.add(e1);
            set.add(e2);
            set.add(e3);
            set.add(e4);
            set.add(e5);
    
            //遍历
            for(Employee e : set){
                System.out.println(e);
            }
    
    
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    测试结果:

    在这里插入图片描述

  • 相关阅读:
    高级IO详解——五种IO模型
    【Vue-Element-Admin】dialog关闭回调事件
    【毕业设计】 基于Stm32的家庭智能监控系统 - 单片机 图像识别 人体检测 AI
    SpringMVC的注解、参数传递、页面跳转
    ES进阶教程
    postgresql数据库结合django设置日期时间默认当前时间
    Lake Formation 和 IAM 之间的区别与联系
    搭建hadoop集群
    Softing mobiLink助力过程自动化——兼容HART、FF、PA的多协议接口工具
    实验27:红外遥控三级控速风扇实验
  • 原文地址:https://blog.csdn.net/qq_46096136/article/details/126717411