import java.util.*;
class TreeSetTest2
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("lisi01",11));
ts.add(new Student("lisi03",51));
ts.add(new Student("lisi04",16));
Iterator it = ts.iterator();
while (it.hasNext())
{
Student stu = (Student)it.next();
sop(stu.getName()+"...."+stu.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 s = (Student)obj;
if(this.name == s.name)
return 1;
if(this.name == s.name)
{
return this.name.compareTo(s.name);
}
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
|