class A {
int count;
public A() {
super();
// TODO Auto-generated constructor stub
}
public A(int count) {
this.count = count;
}
public String toString() {
return "A[count:" + count + "]";
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o != null && A.class == o.getClass()) {
A a = (A) o;
return this.count == a.count;
}
return false;
}
public int hashCode() {
return this.count;
}
}
public class HashSetTest2 {
public static void main(String[] args) {
HashSet hs = new HashSet();
hs.add(new A(4));
hs.add(new A(-3));
hs.add(new A(8));
hs.add(new A(9));
// hs.forEach(System.out::print);
Iterator it = hs.iterator();
A first = (A)it.next();
first.count = 5;//把第一个元素修改为5注意不要修改成与原来集合元素相同的值,这样会造成哈希集合的混乱
hs.forEach(System.out::println);
hs.remove(new A(4));
System.out.println(hs);
}
}