package answer;
import java.util.*;
public class TreeSett {
public static void main(String[] args) {
TreeSet t=new TreeSet();
t.add(new student("kisy",43));
t.add(new student("eisu",43));
Iterator it=t.iterator();
while(it.hasNext())
{
// System.out.println( it.next());
student str=(student)it.next();
System.out.println(str.getName()+"......"+str.getAge()) ;
}
}
}
class student implements Comparable//该接口强制让学生具备比较性。
{
private String name;
private int age;
student(String name,int age)
{
this.name=name;
this.age=age;
}
public int compareTo(Object obj)
{
if(!(obj instanceof student))
throw new RuntimeException("不是学生对象");
student a=(student)obj;
System.out.println(this.name+"...compareto..."+a.name);
if(this.age>a.age)
return 1;
if(this.age==a.age)
{
return this.name.compareTo(a.name);
}
if(this.age==a.age)
return 0;
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
} |