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构造方法的接口参数即可