import java.util.*;
class HashSetDemo
{
public static void main(String[] args)
{
HashSet ha=new HashSet();
ha.add(new Person("张三",22));
ha.add(new Person("李思",23));
ha.add(new Person("王五",24));
ha.add(new Person("李思",23));
ha.add(new Person("王五",24));
// sop(ha);
Iterator it=ha.iterator();
while(it.hasNext())
{
Person p=(Person)it.next();
sop("姓名:"+p.getName()+"年龄:"+p.getAge());
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
class Person
{
private String name;
private int age;
Person(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Person))
return false;
Person p=(Person)obj;
return this.name.equals(p.name) && this.age==p.age;
}
}这里有点迷糊请高手指点讲讲在底层是怎么运行的??? |