import java.util.HashSet;
import java.util.Set;
public class Test {
public static void main(String[] args) throws Exception{
Set set = new HashSet();
Point p1 = new Point("lisi","nanjing");
Point p2 = new Point("wangwu","shanghai");
Point p3 = new Point("zhaoliu","beijing");
set.add(p1);
set.add(p2);
set.add(p3);
System.out.println(set.size());
p1.name = "zhangsan";
set.remove(p1);
System.out.println(set.size());
}
}
class Point{
public String name;
public String address;
public Point(String name,String address){
this.name = name;
this.address = address;
}
public int hashCode() {
return name.hashCode()*address.hashCode();
}
}
我往HashSet集合中添加3个元素,后来想减少一个,为什么还是3个元素呢? |
|