//TreeSet存储自定义对象并遍历练习2(按照姓名的长度排序)
public static void main(String[] args) {
TreeSet<Person> ps =new TreeSet<>();
ps.add(new Person("张三",30));
ps.add(new Person("赵六",24));
ps.add(new Person("王五",22));
ps.add(new Person("李四",21));
System.out.println(ps);
}
/*@Override//compare 重写代码
public int compareTo(Person o) {
int length = this.name.length()-o.name.length(); //比较长度
int num=length==0 ? this.name.compareTo(o.name) : length;//比较内容
return num == 0 ? this.age-o.age : num;//比较年龄
}*/
public static void main(String[] args) {
TreeSet<Person> ps =new TreeSet<>();
ps.add(new Person("张三",30));
ps.add(new Person("赵六",24));
ps.add(new Person("王五",22));
ps.add(new Person("李四",21));
System.out.println(ps);
}
/*@Override//compare 重写代码
public int compareTo(Person o) {
int length = this.name.length()-o.name.length(); //比较长度
int num=length==0 ? this.name.compareTo(o.name) : length;//比较内容
return num == 0 ? this.age-o.age : num;//比较年龄
}*/