上题:
1. 创建MyDate类,
包含:private成员变量year,month,day;并为每一个属性定义 getter,setter方法;
2. 定义一个Employee类。
该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象;
并为每一个属性定义 getter, setter 方法;并重写 toString 方法输出 name, age, birthday
3.创建该类的5个对象,并把这些对象放入TreeSet集合中(下一章:TreeSet需使用泛型来定义)
分别按以下两种方式对集合中的元素进行排序,并遍历输出:
1). 使Employee 实现 Comparable接口,并按name排序
2). 创建 TreeSet时传入Comparator对象,按生日日期的先后排序。
答案代码:
MyDate 类:
package com.atguigu.exer;
/**
* MyDate类包含:
* private成员变量year,month,day;
* 并为每一个属性定义 getter,setter方法;
*
* @author czh
* @create 2020-04-11-19:55
*/
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 year = this.getYear() - m.getYear();
if (year != 0){
return year;
}
//比较月
int month = this.getMonth() - m.getMonth();
if(month != 0){
return month;
}
//比较日
return this.getDay() - m.getDay();
}
throw new RuntimeException("传入的数据类型不一致!");
}
}
Employee 类:
package com.atguigu.exer;
/**
定义一个Employee类。
* 该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象;
* 并为每一个属性定义 getter, setter 方法;
* 并重写 toString 方法输出 name, age, birthday
*
* @author czh
* @create 2020-04-11-19:57
*/
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 +
'}';
}
//按照姓名的顺序排,自然排序
@Override
public int compareTo(Object o) {
if (o instanceof Employee){
Employee e = (Employee)o;
//默认的排序,从小到大
return this.name.compareTo(e.name);
}
throw new RuntimeException("传入的数据有误!");
}
}
EmployeeTest 类:
package com.atguigu.exer;
import org.junit.Test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
创建该类的5个对象,并把这些对象放入TreeSet集合中(下一章:TreeSet需使用泛型来定义)
* 分别按以下两种方式对集合中的元素进行排序,并遍历输出:
*
* 1). 使Employee 实现 Comparable接口,并按name排序
* 2). 创建 TreeSet时传入Comparator对象,按生日日期的先后排序。
*
* @author czh
* @create 2020-04-11-19:59
*/
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();
//调用重写定制排序的compareTo方法
return b1.compareTo(b2);
}
throw new RuntimeException("传入的数据类型不一致!");
}
});
Employee e1 = new Employee("liudehua",25,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",25,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());
}
}
}