日期:2014-05-20 浏览次数:20810 次
import java.util.*;
public class Test
{
public static void main(String[] args)
{
SortedSet<A> list = new TreeSet<A>();
list.add(new A("abc",100));
list.add(new A("efg", 88));
list.add(new A("de", 89));
System.out.println(list);
}
}
class A implements Comparator<A>
{
public A(String name, int number)
{
this.name = name;
this.number = number;
}
public String getName()
{
return name;
}
public int getNumber()
{
return number;
}
public String toString()
{
return "[name=" + name + ", number=" + number + "]";
}
public int compare(A a, A b)
{
if((a.name).compareTo(b.name)>0)
{
return 1;
}
else if((a.name).compareTo(b.name) ==0)
{
return 0;
}
else
{
return -1;
}
}
private String name;
private int number;
}