本帖最后由 罗全涛 于 2012-1-2 22:14 编辑
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection collections = new ArrayList();
ReflectPoint pt1 = new ReflectPoint(3,3);
ReflectPoint pt2 = new ReflectPoint(3,3);
ReflectPoint pt3 = new ReflectPoint(3,3);
collections.clear();
collections.add(pt1);
collections.add(pt2);
collections.add(pt3);
collections.add(pt1);
System.out.println(pt1==pt2);
System.out.println(pt1.equals(pt2));
System.out.println(pt1==pt3);
System.out.println(pt1.equals(pt3));
System.out.println(collections.size());
System.out.println(pt1.hashCode());
System.out.println(pt2.hashCode());
System.out.println(pt3.hashCode());
}
输出结果:
false
false
false
false
4
12677476
33263331
6413875
问题:
1:查询API上说:int hashCode() 返回此 collection 的哈希码值。
那么对collection对象取hashCode值是每个对象的值吗?还是说根本就没意义?
2:如果有意义,如代码中pt1在collection中插入了两次,问题是当我调用
pt1.hashCode()的值的时候它返回的是哪个的hashCode的值?? |