我试图了解树集。为什么下面的代码只打印
a 不是 b 和 c。
下面是代码:
Set<Person> tr = new TreeSet<Person>();
tr.add(new Person("a",1));
tr.add(new Person("c",7));
tr.add(new Person("5",5));
Iterator itr = tr.iterator();
while(itr.hasNext())
{
Person person = (Person) itr.next();
System.out.println(person.getName());
}
人员类
package com.set;
public class Person implements Comparable<Person>{
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name , int age) {
this.name= name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Person o) {
if(age > o.getAge() )
{
return 0;
}else{
return 1;
}
}
}