import java.util.*;
/*
往HashSet集合中存入自定义对象
姓名和年龄一样视为同一个人。
*/
class HashSetText
{
public static void main(String[] args)
{
HashSet hs=new HashSet();
hs.add(new Person("ZhangSan1",21));
hs.add(new Person("ZhangSan2",22));
hs.add(new Person("ZhangSan3",23));
hs.add(new Person("ZhangSan4",24));
hs.add(new Person("ZhangSan5",25));
hs.add(new Person("ZhangSan5",25));
hs.add(new Person("ZhangSan4",23));
for (Iterator it=hs.iterator();it.hasNext() ; )
{
Object o= it.next();
sop(o);
}
}
public static void sop(Object o )
{System.out.println(o);}
}
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+"........"+this.age);
return name.hashCode()+age*5;
}
public boolean equals(Object obj)
{
if (!(obj instanceof Person))
{
return false;
}
Person p=(Person)obj;
// System.out.println(this.name+"______equals_______"+this.age);
return this.name.equals(p.name)&&this.age==p.age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
打印结果:
Person@204d72ac
Person@204d72be
Person@204d72a6
Person@204d72b3
Person@204d72b2
还有一个问题,怎么把DOS命令行的内容复制过来?我都 是复制不过来。 |
|