向TreeSet集合中添加类对象时,类对象的类必须实现Comparable接口,重写compareTo()方法。
两个测试实体类
/**
* 生日实体类
*/
public 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 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 +
'}';
}
}
/**
* Employee实体类
*/
public class Employee implements Comparable<Employee> {
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(Employee o) {
//根据name字段进行排序,从小到大进行排序
return this.name.compareTo(o.name);
//从大到小进行排序
// return o.name.compareTo(this.name);
}
}
1: 自然排序
//使用自然排序对Employee进行排序
//使Employee实现Comparable接口,并按照name字段进行排序
@Test
public void test1() {
//创建一个Set集合
TreeSet<Employee> set = new TreeSet<>();
//创建Employee对象
Employee e1 = new Employee("sanSan", 25, new MyDate(1996, 4, 5));
Employee e2 = new Employee("liSi", 23, new MyDate(1998, 8, 25));
Employee e3 = new Employee("wangWu", 26, new MyDate(1995, 9, 15));
Employee e4 = new Employee("zhaoLiu", 24, new MyDate(1997, 8, 9));
Employee e5 = new Employee("tianQi", 22, new MyDate(1999, 10, 5));
//将Employee对象添加到Set集合中
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
//循环遍历集合
Iterator<Employee> iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
2: 定制排序
//使用定制排序,根据生日字段进行排序,生日从小到大进行排序
@Test
public void test2() {
//创建一个Set集合
TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
//获取到生日
MyDate birthday1 = o1.getBirthday();
MyDate birthday2 = o2.getBirthday();
//比较年,获取到年差
int year = birthday1.getYear() - birthday2.getYear();
if (year != 0) {
return year;
}
//比较月,获取月差
int month = birthday1.getMonth() - birthday2.getMonth();
if (month != 0) {
return month;
}
//比较日,获取日差
return birthday1.getDay() - birthday2.getDay();
}
});
//创建Employee对象
Employee e1 = new Employee("sanSan", 24, new MyDate(1997, 4, 5));
Employee e2 = new Employee("liSi", 23, new MyDate(1998, 8, 25));
Employee e3 = new Employee("wangWu", 26, new MyDate(1995, 9, 15));
Employee e4 = new Employee("zhaoLiu", 24, new MyDate(1997, 8, 9));
Employee e5 = new Employee("tianQi", 22, new MyDate(1999, 10, 5));
//将Employee对象添加到Set集合中
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
//循环遍历集合
Iterator<Employee> iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}