java集合 测试对TreeSet的使用

1.定义一个Employee类。。

该类包含: private 成员变量name,age,birthday, 其中birthday 为MyDate类的对象;。

并为每一个属性定义 getter stter方法:“

并重写toString 方法输出name, age, blrthday.

MyDate类包含:

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

创建该类的5个对象,并把这些对象放入TreeSet 集合中(下一章:TreeSet需使用泛型来定义),

分别按以下两种方式对集合中的元素进行排序,并遍历输出:。

1).使Employee实现Comparable接口,并按name 排序

2).创建TreeSet 时传入Comparator 对象,按生日日期的先后排序。u
 

class ExerciseOne {
    public static void main(String[] args) {
        Employee employee=new Employee("小白", 20, new MyDate(2001, 5, 1));
        Employee employee1=new Employee("GOF", 20, new MyDate(2000, 6, 12));
        Employee employee2=new Employee("白小纯", 20, new MyDate(2001, 7, 12));
        Employee employee3=new Employee("柳青", 20, new MyDate(2001, 9, 12));
        Employee employee4=new Employee("张林", 20, new MyDate(2001, 5, 12));
        Collection treeSet=new TreeSet(new Comparator<>() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Employee&& o2 instanceof Employee){
                    Employee employeeOne=(Employee) o1;
                    Employee employeeTwo=(Employee) o2;
                    int yearOne=employeeOne.getBlrthday().getYear(),yearTwo=employeeTwo.getBlrthday().getYear();
                    int monthOne=employeeOne.getBlrthday().getMonth(),monthTwo=employeeTwo.getBlrthday().getMonth();
                    int dayOne=employeeOne.getBlrthday().getDay(),dayTwo=employeeTwo.getBlrthday().getDay();
                    //大于的情况
                    if (yearOne>yearTwo){
                        return 1;
                    }
                    //等于的情况
                    else if(yearOne==yearTwo){
                            //大于的情况
                            if (monthOne>monthTwo)
                                return 1;
                            //等于的情况
                            else if (monthOne==monthTwo){
                                //大于
                                if (dayOne>dayTwo)
                                    return 1;
                                //等于
                                else if (dayOne==dayTwo){
                                    //如果都想等,则按姓名排序
                                    return employeeOne.getName().compareTo(employeeTwo.getName());
                                }
                                //小于
                                else
                                    return -1;
                            }
                            //小于的情况
                            else {
                                return -1;
                            }

                    }
                   //小于的情况
                    else {
                        return -1;
                    }
                }
                else throw new RuntimeException("不符合类型!");
            }
        });//创建树集合
        treeSet.add(employee);
        treeSet.add(employee1);
        treeSet.add(employee2);
        treeSet.add(employee3);
        treeSet.add(employee4);
        for (Object o : treeSet) {
            System.out.println(o);
        }

    }
}

class Employee implements Comparable{

    private String name;
    private int age;
    private MyDate blrthday;

    public Employee(String name, int age, MyDate blrthday) {
        this.name = name;
        this.age = age;
        this.blrthday = blrthday;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setBlrthday(MyDate blrthday) {
        this.blrthday = blrthday;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public MyDate getBlrthday() {
        return blrthday;
    }

    @Override
    public int compareTo(Object o) {
       if(o instanceof Employee){
           Employee newEmployee=(Employee) o;
          return this.name.compareTo(newEmployee.getName());

       }
       else
           throw new RuntimeException("不符合对象类型!");
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", blrthday=" + blrthday +
                '}';
    }
}




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 void setMonth(int month) {
        this.month = month;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

}

 如果要自然排序,只需要删除TreeSet构造方法的接口参数即可

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

孔雀南飞梦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值