题目
定义一个 Employee 类。
* 该类包含:private 成员变量 name,age,birthday,其中 birthday为MyDate类的对象;
* 并为每一个属性定义 getter, setter 方法;
* 并重写 toString 方法输出 name, age, birthday
* MyDate 类包含:
* private 成员变量 year,month,day;并为每一个属性定义 getter, setter方法;
* 创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中
* 分别按以下两种方式对集合中的元素进行排序,并遍历输出:
* 1). 使 Employee 实现 Comparable 接口,并按 name 排序
* 2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序
代码实现
Employee类
/**
* @author light
* @Description
* 该类包含:private 成员变量 name,age,birthday,其中 birthday为MyDate类的对象;
* 并为每一个属性定义 getter, setter 方法;
* 并重写 toString 方法输出 name, age, birthday
* @create 2023-01-18 21:53
*/
public class Employee implements Comparable {
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 e=(Employee) o;
return this.name.compareTo(e.name);
}
else
throw new RuntimeException("数据类型不匹配");
}
}
MyDate类
package com.light.exer;
/**
* @author light
* @Description
* MyDate 类包含:private 成员变量 year,month,day;
* 并为每一个属性定义 getter, setter
* @create 2023-01-18 21:54
*/
public class MyDate implements Comparable{
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 +
'}';
}
@Override
public int compareTo(Object o) {
if(o instanceof MyDate){
MyDate m2=(MyDate) o;
//判断年
int minusYear=this.getYear()-m2.getYear();
if(minusYear!=0){
return minusYear;
}
//判断月
int minusMonth=this.getMonth()-m2.getMonth();
if(minusMonth!=0){
return minusMonth;
}
//判断日
return this.getDay()-m2.getDay();
}else
throw new RuntimeException("数据类型不匹配");
}
}
TreeSet测试类
import org.junit.Test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
* @author light
* @Description
* 1. 定义一个 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 对象,按生日日期的先后排序
* @create 2023-01-18 21:52
*/
public class TreeSetExer {
//1). 使 Employee 实现 Comparable接口,并按 name 排序
@Test
public void test1(){
TreeSet set = new TreeSet();
Employee e1= new Employee("liudehua",44,new MyDate(1234,4,3));
Employee e2= new Employee("liangqichao",45,new MyDate(1234,5,3));
Employee e3= new Employee("songchao",40,new MyDate(1214,7,3));
Employee e4= new Employee("zhangxueliang",34,new MyDate(1264,4,6));
Employee e5= new Employee("liudehua",44,new MyDate(1234,4,7));
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());
}
}
// 2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序
@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 m1=e1.getBirthday();
MyDate m2=e2.getBirthday();
// //方式一
//
// //判断年
// int minusYear=m1.getYear()-m2.getYear();
// if(minusYear!=0){
// return minusYear;
// }
//
// //判断月
// int minusMonth=m1.getMonth()-m2.getMonth();
// if(minusMonth!=0){
// return minusMonth;
// }
//
// //判断日
// return m1.getDay()-m2.getDay();
//方式二
return m1.compareTo(m2);
}else
throw new RuntimeException("数据类型不匹配");
}
});
Employee e1= new Employee("liudehua",44,new MyDate(1234,4,3));
Employee e2= new Employee("liangqichao",45,new MyDate(1234,5,3));
Employee e3= new Employee("songchao",40,new MyDate(1214,7,3));
Employee e4= new Employee("zhangxueliang",34,new MyDate(1264,4,6));
Employee e5= new Employee("liudehua",44,new MyDate(1234,4,7));
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());
}
}
}
test1运行结果
test2运行结果