import java.util.Set;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
Set<Student> set = new TreeSet<Student>();
set.add(new Student("1",21));
set.add(new Student("2",22));
System.out.println(set);
}
}
class Student implements Comparable<Student> {
private String name;
private int age;
Student(String name,int age) {
this.name = name;
this.age = age;
}
public Student(String string) {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public int compareTo(Student stu) {
return stu.getAge() - this.getAge();
}
}
你的代买没写全,看看这个,没问题的,希望能帮到您 |