Java中关于使用TreeSet存储数据的自然排序和定制排序

一、题目

创建类的 5 个对象,并把这些对象放入 TreeSet 集合中(TreeSet 需使用泛型和不用泛型分别来定义)
分别按以下两种方式对集合中的元素进行排序,并遍历输出:

  1. 使 Employee 实现 Comparable 接口,并按 name 排序
  2. 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。

二、定义一个 Employee 类

/**
 * 该类包含:private 成员变量 name,age,birthday,其中 birthday 为
 * MyDate 类的对象;
 * 并为每一个属性定义 getter, setter 方法;
 * 并重写 toString 方法输出 name, age, birthday
 * @author
 * @create 2021-01-22-15:00
 */
public class Employee implements Comparable<Employee> {
    private String name;
    private int age;
    private MyDate birthday;

    public Employee() {
    }

    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 String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
     //不用泛型
//    @Override
//    public int compareTo(Object o) {
//        if(o instanceof Employee){
//            Employee employee = (Employee) o;
//            return this.name.compareTo(employee.name);
//        }
//        throw new RuntimeException("输入的数据类型不一致");
//    }
     //使用泛型
    @Override
    public int compareTo(Employee o) {
        return this.name.compareTo(o.name);
    }
}

三、MyDate 类

/**
 * MyDate 类包含:
 * private 成员变量 year,month,day;并为每一个属性定义 getter, setter
 * 方法;
 * @author 
 * @create 2021-01-22-15:00
 */
public class MyDate implements Comparable<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;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", 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 int compareTo(MyDate o) {
        int minusYear= this.year-o.year;
        if (minusYear !=0){
            return minusYear;
        }
        int minusMonth= this.month-o.month;
        if (minusMonth !=0){
            return minusMonth;
        }
        return this.day-o.day;
    }
}

四、单元测试

(一)

@Test
    public void test1(){
        TreeSet<Employee> set = new TreeSet<>();

        set.add(new Employee("hh",23,new MyDate(1992,4,12)));
        set.add(new Employee("ff",43,new MyDate(1956,5,4)));
        set.add(new Employee("aa",27,new MyDate(1936,8,6)));
        set.add(new Employee("gg",38,new MyDate(1992,4,4)));

        Iterator<Employee> iterator = set.iterator();

        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

结果如下:
在这里插入图片描述

(二)

 @Test
    public void test2(){
        TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
            @Override
            public int compare(Employee e1, Employee e2) {
                //加上泛型
                MyDate b1 = e1.getBirthday();
                MyDate b2 = e2.getBirthday();
                return b1.compareTo(b2);


                //不加泛型
//                if (o1 instanceof Employee && o2 instanceof Employee){
//                    Employee m1 = (Employee) o1;
//                    Employee m2 = (Employee) o2;
//                    MyDate m1Birthday = m1.getBirthday();
//                    MyDate m2Birthday = m2.getBirthday();
//
//                    int minusYear = m1Birthday.getYear()- m2Birthday.getYear();
//                    if (minusYear!=0){
//                        return minusYear;
//                    }
//                    int minusMonth = m1Birthday.getMonth()- m2Birthday.getMonth();
//                    if (minusMonth!=0){
//                        return minusMonth;
//                    }
//                    int minusDay = m1Birthday.getDay()- m2Birthday.getDay();
//                    return minusDay;
//
//                }
//                throw new RuntimeException("传入的数据类型不一致");
            }
        });

        set.add(new Employee("hh",23,new MyDate(1944,12,4)));
        set.add(new Employee("ff",43,new MyDate(1957,5,4)));
        set.add(new Employee("aa",27,new MyDate(1906,12,6)));
        set.add(new Employee("gg",38,new MyDate(1906,4,4)));

        Iterator<Employee> iterator = set.iterator();

        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

结果如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值