System.out.println("-----------------------");
Set<Person> set2 = new TreeSet<Person>(new MyComparable());
set2.add(new Person("a", 79));
set2.add(new Person("b", 79));
set2.add(new Person("c",69));
set2.add(new Person("d", 59));
set2.add(new Person("e", 60));
System.out.println(set2);
}
}
class MyComparable implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {//创建给perosn里的集合排序,Trreset集合
int arr = o1.getAge() - o2.getAge();
return arr == 0 ? arr : o1.getName().compareTo(o2.getName());
}
}
class StringComparable implements Comparator<String>{//给集合倒序
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);//正序和倒叙, 只改一下O1和o2
}
}
class IntergComparable implements Comparator<Integer>{//给集合倒序
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);//正序和倒叙, 只改一下O1和o2
}
}
[Person = a 70, Person = a 79, Person = b 79, Person = c 69, Person = d 59, Person = e 60]
-----------------------
[Person = d 59, Person = e 60, Person = c 69, Person = a 79, Person = b 79]