本帖最后由 邱成 于 2012-10-1 23:17 编辑
import java.util.*;
public class HashSetTest
{
public static void main(String[] args){
HashSet hs=new HashSet();
hs.add(new Student(1,"zhangsan"));
hs.add(new Student(2,"lishi"));
hs.add(new Student(3,"wangwu"));
hs.add(new Student(1,"zhangsan"));
Iterator it=hs.iterator();
while (it.hasNext())
{System.out.println(it.next());
}
}
public class Student
{
int num;
String name;
Student(int num,String name){
this.num=num;
this.name=name;
}
public String toString(){
return "num :"+num+" name:"+name;
}
public int hashCode(){
return num*name.hashCode();
}
public boolean equals(Object o){
Student s=(Student)o;
return num==s.num&&name.equals(s.name);
}
}
}
请大家帮忙看下问题出在哪里? |