哪位大神能帮我看看,谢了
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有提供....