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

②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 +
                '}';
    }
}

③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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五度鱼学Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值