定义Employee类
1. 该类包括:private name/sal/birth 其中birth为 MyDate类 对象
2. 为每一个属性定义 getter setter方法
3. 重写 toString 输出三个元素
4. MyDate类 包含:private成员变量 month day year 并为每一个属性定义 getter setter
5. 创建Employee类 的 3个 对象 并把这些对象放入 ArrayList集合 中 (
ArrayList需用 泛型来定义) 对集合中的元素进行排序 遍历输出
排序方法:调用ArrayList的sort方法 传入 Comparator对象(使用泛型)
先按照name排序
如果name相同 则按照生日日期的先后排序
public class Ex02 {
public static void main(String[] args) {
/*
定义Employee类
1. 该类包括:private name/sal/birth 其中birth为 MyDate类 对象
2. 为每一个属性定义 getter setter方法
3. 重写 toString 输出三个元素
4. MyDate类 包含:private成员变量 month day year 并为每一个属性定义 getter setter
5. 创建Employee类 的 3个 对象 并把这些对象放入 ArrayList集合 中 (
ArrayList需用 泛型来定义) 对集合中的元素进行排序 遍历输出
排序方法:调用ArrayList的sort方法 传入 Comparator对象(使用泛型)
先按照name排序
如果name相同 则按照生日日期的先后排序
*/
ArrayList<Employee> employees = new ArrayList<>();
employees.add(new Employee("2", 222, new MyDate(2, 2, 2)));
employees.add(new Employee("3", 333, new MyDate(3, 3, 3)));
employees.add(new Employee("3", 333, new MyDate(3, 2, 3)));
employees.add(new Employee("1", 111, new MyDate(1, 1, 1)));
employees.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
// if ((o1.getName()).compareTo(o2.getName()) == 0) {
// if (o1.getBirth().getYear() == o2.getBirth().getYear()) {
// if (o1.getBirth().getMonth() == o2.getBirth().getMonth()) {
// return o1.getBirth().getDay() - o2.getBirth().getDay();
// }
// return o1.getBirth().getMonth() - o2.getBirth().getMonth();
// }
// return o1.getBirth().getYear() - o2.getBirth().getYear();
// } else {
// return (o1.getName()).compareTo(o2.getName());
// }
//比较 name:
int i = (o1.getName()).compareTo(o2.getName());
if (i != 0) {
return i;
}
//对birthday的比较 把它具体的算法 放在MyDate类 里完成:
return o1.getBirth().compareTo(o2.getBirth());
}
});
for (Employee emp : employees) {
System.out.println(emp);
}
}
}
class Employee{
private String name;
private double sal;
private MyDate birth;
public Employee(String name, double sal, MyDate birth) {
this.name = name;
this.sal = sal;
this.birth = birth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public MyDate getBirth() {
return birth;
}
public void setBirth(MyDate birth) {
this.birth = birth;
}
@Override
public String toString() {
return "name: '" + name + '\'' +
", sal: " + sal +
", birth: " + birth;
}
}
class MyDate implements Comparable<MyDate> {
private int month;
private int day;
private int year;
public MyDate(int month, int day, int year) {
this.month = month;
this.day = day;
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;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "month= " + month +
", day= " + day +
", year= " + year;
}
@Override
public int compareTo(MyDate o) {
int yearMinus = year - o.getYear();
if (yearMinus != 0) {
return yearMinus;
}
int monthMinus = month - o.getMonth();
if (monthMinus != 0) {
return monthMinus;
}
return day - o.getDay();
}
}