修改如下:
import java.util.*;
class Cjd
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new);
ts.add(new Student("李雷",18,95));
ts.add(new Student("韩梅梅",19,97));
ts.add(new Student("露西",17,96));
ts.add(new Student("莉莉",17,94));
ts.add(new Student("汤姆",18,94));
Iterator it = ts.iterator( new compartorByResult);
while(it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+" "+stu.getAge()+" "+stu.getResult()+"\t");
}
}
}
class Student {//implements Comparable 这里实现这个借口比必要了
private String name;
private int age;
private int result;
Student(String name,int age,int result)
{
this.name=name;
this.age=age;
this.result=result;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public void setResult(int result)
{
this.result=result;
}
class compartorByLevel implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Student s1 = (Result)o1;
Student s2 = (Result)o2;
int temp = s2.getResult()-s1.getResult();
return temp==0?s1.getName().compareTo(s2.getName()):temp;
}
} |