为什么系列之重写equals方法必须重写hasCode方法?

Object源代码及注释

equals是Object的公有方法,那么我们通常都会在自己的类中重写这个equals方法,同时必须重写hasCode方法,知道为什么重写equals方法必须重写hasCode方法呢?

公司主营业务:成都网站设计、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出志丹免费做网站回馈大家。

/**
     * Returns a hash code value for the object. This method is
     * supported for the benefit of hash tables such as those provided by
     * {@link java.util.HashMap}.
     * 

* The general contract of {@code hashCode} is: *

    *
  • Whenever it is invoked on the same object more than once during * an execution of a Java application, the {@code hashCode} method * must consistently return the same integer, provided no information * used in {@code equals} comparisons on the object is modified. * This integer need not remain consistent from one execution of an * application to another execution of the same application. *
  • If two objects are equal according to the {@code equals(Object)} * method, then calling the {@code hashCode} method on each of * the two objects must produce the same integer result. *
  • It is not required that if two objects are unequal * according to the {@link java.lang.Object#equals(java.lang.Object)} * method, then calling the {@code hashCode} method on each of the * two objects must produce distinct integer results. However, the * programmer should be aware that producing distinct integer results * for unequal objects may improve the performance of hash tables. *
*

* As much as is reasonably practical, the hashCode method defined by * class {@code Object} does return distinct integers for distinct * objects. (This is typically implemented by converting the internal * address of the object into an integer, but this implementation * technique is not required by the * Java™ programming language.) * * @return a hash code value for this object. * @see java.lang.Object#equals(java.lang.Object) * @see java.lang.System#identityHashCode */ public native int hashCode(); /** * Indicates whether some other object is "equal to" this one. *

* The {@code equals} method implements an equivalence relation * on non-null object references: *

    *
  • It is reflexive: for any non-null reference value * {@code x}, {@code x.equals(x)} should return * {@code true}. *
  • It is symmetric: for any non-null reference values * {@code x} and {@code y}, {@code x.equals(y)} * should return {@code true} if and only if * {@code y.equals(x)} returns {@code true}. *
  • It is transitive: for any non-null reference values * {@code x}, {@code y}, and {@code z}, if * {@code x.equals(y)} returns {@code true} and * {@code y.equals(z)} returns {@code true}, then * {@code x.equals(z)} should return {@code true}. *
  • It is consistent: for any non-null reference values * {@code x} and {@code y}, multiple invocations of * {@code x.equals(y)} consistently return {@code true} * or consistently return {@code false}, provided no * information used in {@code equals} comparisons on the * objects is modified. *
  • For any non-null reference value {@code x}, * {@code x.equals(null)} should return {@code false}. *
*

* The {@code equals} method for class {@code Object} implements * the most discriminating possible equivalence relation on objects; * that is, for any non-null reference values {@code x} and * {@code y}, this method returns {@code true} if and only * if {@code x} and {@code y} refer to the same object * ({@code x == y} has the value {@code true}). *

* Note that it is generally necessary to override the {@code hashCode} * method whenever this method is overridden, so as to maintain the * general contract for the {@code hashCode} method, which states * that equal objects must have equal hash codes. * * @param obj the reference object with which to compare. * @return {@code true} if this object is the same as the obj * argument; {@code false} otherwise. * @see #hashCode() * @see java.util.HashMap */ public boolean equals(Object obj) { return (this == obj); }

上面是Object对象中hasCode和equals的签名和方法说明。

为啥重写equals方法

判断一些对象是否等于当前对象,实现在非空对象映射的等价关系。this==obj,由此可知,这是一个引用对象的等价判断,只有在this和obj是指向用一个应用时才返回true.这是判定实例的唯一性,我们通常使用equals时,其实并不想判断实例的唯一性,而是想要比较里面的几个(自定义)属性值是否相等,来判断是否为同一个"东西"。

契约:
reflexive(自反性):x为非null时,x.equals(x),一定返回true.
symmetric(对称性):x,y都不为null,如果x.equals(y)返回true,那么y.equals(x).
transitive(传递性):x,y,z都不为null,如果x.equals(y),y.equals(z)返回true,那么x.equals(z)返回true.
consistent(一致性):对于任何非null的x\y,在没有修改这些对象信息的前提下,多次调用下,x.equals(y)结果是一致的。
非null的x,x.equals(null)返回false.

下面是Book类的equals重写实现,通过bookN、bookName确定是否为同一本书(东西),而不是判断两个对象的引用是否为同一个。

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return  Objects.equal(bookNo, book.bookNo) &&
                Objects.equal(bookName, book.bookName);
    }

为啥重写hasCode方法

hasCode锲约:
1.任何时候在没有修改对象的前提下,多次调用同一个对象的hasCode方法,这个返回值必须是同一个Integer值。
2.如果两个对象equals结果为true,那么hasCode的值必须相同。
3.如果两个对象equals结果为false,那么hasCode就不一定不相同。

如果重写equals方法时,不去重写hasCode方法,那么必然会违背第二条锲约。所以每次hasCode必须跟随equals重写而重写。

扩展知识

是不是有人就是问:如果就不重写hasCode,不行吗?就违背锲约不可以吗?只equals重写有什么问题吗?
那么这里就会引申出hasCode在集合中的应用场景。

Java中的集合(Collection)有两类,一类是List,再有一类是Set。前者集合内的元素是有序的,元素可以重复;后者元素无序,但元素不可重复。那么这里就有一个比较严重的问题了:要想保证元素不重复,可两个元素是否重复应该依据什么来判断呢?这就是 Object.equals方法了。但是,如果每增加一个元素就检查一次,那么当元素很多时,后添加到集合中的元素比较的次数就非常多了。也就是说,如果集合中现在已经有1000个元素,那么第1001个元素加入集合时,它就要调用1000次equals方法。这显然会大大降低效率。

于是,Java采用了哈希表的原理。哈希算法也称为散列算法,是将数据依特定算法直接指定到一个地址上。可以这样简单理解,hashCode方法实际上返回的就是对象存储位置的映像。

当集合中添加元素时,先调用hasCode方法,如果这个位置没有元素,那么就直接存储在这里,如果有元素,那么再调用equals方法与新元素比较,如果相同不存了,如果不相同则说明发生碰撞(冲突),碰撞解决方法多样,最终都会存储在一个合适的位置。有了hasCode后,调用equals的次数大大降低,一般一两次就搞定了。

扩展的内容有兴趣可以自行深入学习。为什么系列采用简短,易懂方式归纳总结一些工作中、面试中常常出现的问题进行解答。如果有错误请及时联系我,以免误导他人。


网页标题:为什么系列之重写equals方法必须重写hasCode方法?
URL地址:http://scyanting.com/article/pdcjpg.html