11-5 Java集合---- Collection子接口之二: Set接口(3)TreeSet课后练习

本文详细介绍了如何在Java中使用Employee和MyDate类实现Comparable接口进行name排序,以及通过Comparator进行生日日期排序。通过实例演示了如何将5个对象添加到TreeSet并按指定规则进行排序和遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

11-5 Java集合---- Collection子接口之二: Set接口(3)TreeSet课后练习

定义一个 Employee 类。
该类包含:private 成员变量 name,age,birthday,其中 birthday 为MyDate 类的对象;
并为每一个属性定义 getter, setter 方法;
并重写 toString 方法输出 name, age, birthday

MyDate 类包含:
private 成员变量 year,month,day;并为每一个属性定义 getter, setter 方法;

创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(下一章:TreeSet 需使用泛型来定义)
分别按以下两种方式对集合中的元素进行排序,并遍历输出:
(1) 使 Employee 实现 Comparable 接口,并按 name 排序
(2)创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。

Employee类:

package exer1;


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

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

    }

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

    //按 name 排序
    @Override
    public int compareTo(Object o) {
        if (o instanceof Employee) {
            Employee e = (Employee) o;
            return this.name.compareTo(e.name);
        }
//        return 0;
        throw new RuntimeException("传入的数据类型不一致!");
    }
}

MyDate类:

package exer1;

/**
 * MyDate类包含:
 * private成员变量year,month,day;并为每一个属性定义 getter, setter 方法;
 */
public class MyDate implements Comparable {
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public MyDate() {

    }

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

    @Override
    public int compareTo(Object o) {
        if (o instanceof MyDate) {
            MyDate m = (MyDate) o;

            //比较年
            int minusYear = this.getYear() - m.getYear();
            if (minusYear != 0) {
                return minusYear;
            }
            //比较月
            int minusMonth = this.getMonth() - m.getMonth();
            if (minusMonth != 0) {
                return minusMonth;
            }
            //比较日
            return this.getDay() - m.getDay();
        }

        throw new RuntimeException("传入的数据类型不一致!");

    }
}

EmployeeTest:

package exer1;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * 创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(下一章:TreeSet 需使用泛型来定义)
 * 分别按以下两种方式对集合中的元素进行排序,并遍历输出:
 * <p>
 * 1). 使Employee 实现 Comparable 接口,并按 name 排序
 * 2). 创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。
 */
public class EmployeeTest {

    //问题二:按生日日期的先后排序。
    @Test
    public void test2() {


        TreeSet set = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Employee && o2 instanceof Employee) {
                    Employee e1 = (Employee) o1;
                    Employee e2 = (Employee) o2;

                    MyDate b1 = e1.getBirthday();
                    MyDate b2 = e2.getBirthday();
                    //方式一:
//                    //比较年
//                    int minusYear = b1.getYear() - b2.getYear();
//                    if(minusYear != 0){
//                        return minusYear;
//                    }
//                    //比较月
//                    int minusMonth = b1.getMonth() - b2.getMonth();
//                    if(minusMonth != 0){
//                        return minusMonth;
//                    }
//                    //比较日
//                    return b1.getDay() - b2.getDay();

                    //方式二:
                    return b1.compareTo(b2);

                }
//                return 0;
                throw new RuntimeException("传入的数据类型不一致!");
            }
        });

        Employee e1 = new Employee("liudehua", 55, new MyDate(1965, 5, 4));
        Employee e2 = new Employee("zhangxueyou", 43, new MyDate(1987, 5, 4));
        Employee e3 = new Employee("guofucheng", 44, new MyDate(1987, 5, 9));
        Employee e4 = new Employee("liming", 51, new MyDate(1954, 8, 12));
        Employee e5 = new Employee("liangzhaowei", 21, new MyDate(1978, 12, 4));

        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);

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


    //问题一:使用自然排序
    @Test
    public void test1() {
        TreeSet set = new TreeSet();

        Employee e1 = new Employee("liudehua", 55, new MyDate(1965, 5, 4));
        Employee e2 = new Employee("zhangxueyou", 43, new MyDate(1987, 5, 4));
        Employee e3 = new Employee("guofucheng", 44, new MyDate(1987, 5, 9));
        Employee e4 = new Employee("liming", 51, new MyDate(1954, 8, 12));
        Employee e5 = new Employee("liangzhaowei", 21, new MyDate(1978, 12, 4));

        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);

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

输出:

Employee{name='guofucheng', age=44, birthday=MyDate{year=1987, month=5, day=9}}
Employee{name='liangzhaowei', age=21, birthday=MyDate{year=1978, month=12, day=4}}
Employee{name='liming', age=51, birthday=MyDate{year=1954, month=8, day=12}}
Employee{name='liudehua', age=55, birthday=MyDate{year=1965, month=5, day=4}}
Employee{name='zhangxueyou', age=43, birthday=MyDate{year=1987, month=5, day=4}}
Employee{name='liming', age=51, birthday=MyDate{year=1954, month=8, day=12}}
Employee{name='liudehua', age=55, birthday=MyDate{year=1965, month=5, day=4}}
Employee{name='liangzhaowei', age=21, birthday=MyDate{year=1978, month=12, day=4}}
Employee{name='zhangxueyou', age=43, birthday=MyDate{year=1987, month=5, day=4}}
Employee{name='guofucheng', age=44, birthday=MyDate{year=1987, month=5, day=9}}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

YY鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值