A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

©   /  2013-4-2 17:08  /  2204 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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>.

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马