- class HashSetTest
- {
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- public static void main(String[] args)
- {
- HashSet hs = new HashSet();
- hs.add(new Person("a1",11));
- hs.add(new Person("a2",12));
- hs.add(new Person("a3",13));
- // hs.add(new Person("a2",12));
- // hs.add(new Person("a4",14));
- //sop("a1:"+hs.contains(new Person("a2",12)));
- // hs.remove(new Person("a4",13));
- Iterator it = hs.iterator();
- while(it.hasNext())
- {
- Person p = (Person)it.next();
- sop(p.getName()+"::"+p.getAge());
- }
- }
- }
- class Person
- {
- private String name;
- private int age;
- Person(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public int hashCode()
- {
- System.out.println(this.name+"....hashCode");
- return name.hashCode()+age*37;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Person))
- return false;
- Person p = (Person)obj;
- System.out.println(this.name+"...equals.."+p.name);
- return this.name.equals(p.name) && this.age == p.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;
- System.out.println(this.name+"...equals.."+p.name);
- return this.name.equals(p.name) && this.age == p.age;
- }
复制代码 明明在主函数中已经做强制转换了呀,
while(it.hasNext())
{
Person p = (Person)it.next();
sop(p.getName()+"::"+p.getAge());
}
应该不用判断了呀!
而且毕老师的写的这个函数也没有在这个程序里调用过。这样做是出于什么考虑呢?
望高人相告!!!!!
|