import java.util.*;
class Student implements Comparable{
String name;
int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return name+":"+age;
}
public int comparaeTo(Object obj){
Student s=(Student) obj;
if(this.age-s.age>0){
return 1;
}
if(this.age-s.age==0){
return this.name.compareTo(s.name);
}
return -1;
}
}
public class Demo7{
public static void main(String[] args){
TreeSet ts=new TreeSet(); //创建HashSet集合
ts.add(new Student("zhangsan",19));
ts.add(new Student("lisi",18));
ts.add(new Student("wangwu",19));
ts.add(new Student("zhangsan",19));
Iterator it=ts.iterator();
while(it.hasNext()){
System.out.println(it.next);
}
}
} |