定义Employee类
1) 该类包含:private成员变量name,sal, birthday, 其中 birthday 为 MyDate 类的对象;
2) 为每一个属性定义 getter, setter 方法;
3)重写 toString 方法输出 name, sal, birthday
4) MyDate类包含:private成员变量month,day.year:并为每一个属性定义 getter,setter 方法;
5) 创建该类的3个对象,并把这些对象放入 ArrayList 集合中(ArrayList 需使用泛型来定义),对集合中的元素进行排序,并遍历输出:
排序方式:调用ArrayList 的sort 方法,传入Comparator对象[使用泛型],先按照name排序,如果name相同,则按生日日期的先后排序。
【即:定制排序)有一定难度,比较经典 泛型使用案例
第一次做:做到遍历用sort时候不知道怎么做了
package com.msbedu.generic;
import java.util.ArrayList;
import java.util.Comparator;
public class generic03 {
public static void main(String[] args) {
ArrayList<Employee> arrayList = new ArrayList<>();
arrayList.add(new Employee("小赞", 1000, new MyDate(9, 3, 2024)));
arrayList.add(new Employee("小美", 2500, new MyDate(5, 6, 2024)));
arrayList.add(new Employee("小花", 3000, new MyDate(7, 8, 2024)));
arrayList.sort(new Comparator<Employee>() {
@Override
public int compare(Employee ep1, Employee ep2) {
return 0;
//做到这一步逻辑开始混乱
}
});
}
}
class Employee {
private String name;
private int sal;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Employee(String name, int sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
}
class MyDate {
private int month;
private int day;
private int 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;
}
public MyDate(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
@Override
public String toString() {
return "MyDate{" +
"month=" + month +
", day=" + day +
", year=" + year +
'}';
}//MyDate的toString也需要重写
}
看视频,第二次:
package com.msbedu.generic;
import java.util.ArrayList;
import java.util.Comparator;
public class generic03 {
public static void main(String[] args) {
ArrayList<Employee> arrayList = new ArrayList<>();
arrayList.add(new Employee("小赞", 1000, new MyDate(9, 3, 2024)));
arrayList.add(new Employee("小美", 2500, new MyDate(5, 6, 2024)));
arrayList.add(new Employee("小花", 3000, new MyDate(7, 8, 2024)));
//排序并遍历
// 排序方式:调用ArrayList 的sort 方法,传入Comparator对象[使用泛型],
arrayList.sort(new Comparator<Employee>() {
@Override
public int compare(Employee ep1, Employee ep2) {
//先按照name排序,如果name相同,则按生日日期的先后排序。
//先对传入的参数进行验证 如果是Employee则继续
if (!(ep1 instanceof Employee && ep2 instanceof Employee)) {
System.out.println("类型不正确...");
return 0;
}
//类型正确判断名字
int nameCompare = ep1.getName().compareTo(ep2.getName());
if (nameCompare != 0) {
return nameCompare;
}
//name相同,比较year、month、day
//先比较year
int yearminus = ep1.getBirthday().getYear() - ep2.getBirthday().getYear();
if (yearminus != 0) {
return yearminus;
}
//year相同,比较month
int monthminus = ep1.getBirthday().getMonth() - ep2.getBirthday().getMonth();
if (monthminus != 0) {
return monthminus;
}
//year、month相同,比较day
return ep1.getBirthday().getDay() - ep2.getBirthday().getDay();
}
});
System.out.println("比较后"+arrayList);
}
}
class Employee {
private String name;
private int sal;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Employee(String name, int sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee:" +
"Name: " + name +
"Salary: " + sal +
"Birthday: " + birthday + "\n";
}
}
class MyDate {
private int month;
private int day;
private int 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;
}
public MyDate(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
@Override
public String toString() {
return "MyDate{" +
"month=" + month +
", day=" + day +
", year=" + year +
'}';
}
}
该方案代码仍有冗余,主要是实现比较的方法应该在MyDate实现
第三次:
package com.msbedu.generic;
import java.util.ArrayList;
import java.util.Comparator;
public class generic03 {
public static void main(String[] args) {
ArrayList<Employee> arrayList = new ArrayList<>();
arrayList.add(new Employee("小赞", 1000, new MyDate(9, 3, 2024)));
arrayList.add(new Employee("小美", 2500, new MyDate(5, 6, 2024)));
arrayList.add(new Employee("小花", 3000, new MyDate(7, 8, 2024)));
//排序并遍历
// 排序方式:调用ArrayList 的sort 方法,传入Comparator对象[使用泛型],
arrayList.sort(new Comparator<Employee>() {
@Override
public int compare(Employee ep1, Employee ep2) {
//先按照name排序,如果name相同,则按生日日期的先后排序。
//先对传入的参数进行验证 如果是Employee则继续
if (!(ep1 instanceof Employee && ep2 instanceof Employee)) {
System.out.println("类型不正确...");
return 0;
}
//类型正确判断名字
int nameCompare = ep1.getName().compareTo(ep2.getName());
if (nameCompare != 0) {
return nameCompare;
}
//将年月日的比较通过接口实现 在MyDate中比较
//在这只需要比较两个MyDate对象
return ep1.getBirthday().compareTo(ep2.getBirthday());
}
});
System.out.println("比较后" + arrayList);
}
}
class Employee {
private String name;
private int sal;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Employee(String name, int sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"Name= " + name +
",Salary= " + sal +
",Birthday= " + birthday + "\n";
}
}
class MyDate implements Comparable<MyDate> {
private int month;
private int day;
private int 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;
}
public MyDate(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
@Override
public String toString() {
return "MyDate{" +
"month=" + month +
", day=" + day +
", year=" + year +
'}';
}
@Override
public int compareTo(MyDate o) {
//name相同,比较year、month、day
//先比较year
int yearminus = year - o.getYear();
if (yearminus != 0) {
return yearminus;
}
//year相同,比较month
int monthminus = month - o.getMonth();
if (monthminus != 0) {
return monthminus;
}
//year、month相同,比较day
return day - o.getDay();
}
}
class MyDate implements Comparable<MyDate>重写了compareTo方法,做到将比较年月日放在MyDate中,体现了java面向对象的思想,测试类中只需要通过调用员工对象的getBirthday
方法获取生日对象,然后调用生日对象的compareTo
方法进行比较。这个方法在MyDate
类中实现,按照年份、月份和日期的先后顺序进行比较。
自己最后一步悟不到,还停留在第二步刚进入。