当前位置: 代码迷 >> J2SE >> 大神们
  详细解决方案

大神们

热度:8570   发布时间:2013-02-25 00:00:00.0
求救大神们!

哪位大神能帮我看看,谢了




import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class HashSetTest<T>
{
public static void main(String[] args)
{
Set<Person> set = new HashSet<Person>(new MyComparator1());//这行代码错了,为什么?
set.add(new Person("mipaifu", 19, "shantou"));
set.add(new Person("yuangupo", 17, "xiandi"));

System.out.println("Name" + '\t' + "Age" + '\t' + "Address");

for (Iterator<Person> iter = set.iterator(); iter.hasNext();)
{
Person person = iter.next();
System.out.println(person);
}
}
}

class Person
{
String name;
int age;
String address;

public Person(String name, int age, String address)
{
this.name = name;
this.age = age;
this.address = address;
}

public String toString()
{
return (name + '\t' + age + '\t' + address);
}
}

class MyComparator1 implements Comparator<Person>
{

@Override
public int compare(Person o1, Person o2)
{
return o2.age - o1.age;
}

}


------解决方案--------------------------------------------------------
HashSet构造参数中没有排序这以功能
TreeSet有提供....
  相关解决方案