//HashSet的测试
import java.util.*;
public class HashSetTest
{
public static void main(String[] args)
{
HashSet ha = new HashSet();
StudentTwo st1=new StudentTwo("zhangsan",18);
StudentTwo st2=new StudentTwo("lisi",17);
StudentTwo st3=new StudentTwo("wangwu",19);
StudentTwo st4=new StudentTwo("zhangsan",18);
/*ha.add("one");
ha.add("two");
ha.add("three");
ha.add("one");*/
ha.add(st1);
ha.add(st2);
ha.add(st3);
ha.add(st4);
Iterator i = ha.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
class StudentTwo
{
int num;
String name;
StudentTwo(String name,int num)
{
this.num=num;
this.name=name;
}
public String toString()
{
return num+":"+name;
}
public int hashCode()
{
return num*name.hashCode();
}
public boolean equals(Object obj)
{
StudentTwo s=(StudentTwo)obj;
return (num==s.num)&(name.equals(s.name));
}
}
3299

被折叠的 条评论
为什么被折叠?



