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



    }
}

测试结果:

在这里插入图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>