import java.util.TreeSet;
class Student implements Comparable{
private String name;
private int age;
public Student (String name,int age) {
this.name=name;
this.age=age;
}
public String toString() {
return name+":"+age;
}
//重写Comparable接口的compareTo()方法
public int compareTo(Object obj) {
Student stu=(Student)obj;
if(this.age-stu.age>0) {
return 1;
}
if(this.age==stu.age) {
return this.name.compareTo(stu.name);
}
return -1;
}
}
public class Example {
public static void main(String[]args) {
TreeSet ts=new TreeSet();
ts.add(new Student("Lucy",18));
ts.add(new Student("Tom",20));
ts.add(new Student("Bob",20));
ts.add(new Student("Tom",20));
System.out.println(ts);
}
}
运行结果:
[Lucy:18, Bob:20, Tom:20]
注:首先按年龄升序,年龄相同按照名字升序,并去掉重复元素。