equals比较得是两个对象,或者是两个对象的内容,假如两个对象相等那它们两个的hashcode也必须一样,因为hashcode是用对象的内部地址转换成一个整数的。
假如你改写了equals方法而没重写hashcode方法,那就乱套了!
这是sun说的,看看Object#equals()和Object#hashCode()的文档
* Note that it is generally necessary to override the <tt>hashCode</tt>
* method whenever this method is overridden, so as to maintain the
* general contract for the <tt>hashCode</tt> method, which states
* that equal objects must have equal hash codes.
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <code>java.util.Hashtable</code>.
|