未验证 提交 af038f90 编写于 作者: Linky1990's avatar Linky1990 提交者: GitHub

Update 11. 重写equals方法时同时也要重写hashcode方法.md (#63)

上级 c7c3483c
# 11. 重写 equals 方法时同时也要重写 hashcode 方法
  **在每个类中,在重写 equals 方法的时侯,一定要重写 hashcode 方法。** 如果不这样做,你的类违反了 hashCode 的通用约定,这会阻止它在 HashMap 和 HashSet 这样的集合中正常工作。根据 Object 规范,以下具体约定。
  **在每个类中,在重写 equals 方法的时侯,一定要重写 hashcode 方法。** 如果不这样做,你的类违反了 hashCode 的通用约定,这会阻止它在 HashMap 和 HashSet 这样的集合中正常工作。根据 Object 规范,以下具体约定。
1. 如果没有修改 equals 方法中用以比较的信息,在应用程序的一次执行过程中对一个对象重复调用 hashCode 方法时,它必须始终返回相同的值。在应用程序的多次执行过程中,每个执行过程在该对象上获取的结果值可以不相同。
2. 如果两个对象根据 equals(Object) 方法比较是相等的,那么在两个对象上调用 hashCode 就必须产生的结果是相同的整数。
......@@ -104,4 +104,4 @@ public int hashCode() {
[1]: http://com.google.common.hash.hashing/
[1]: https://github.com/google/guava/blob/master/guava/src/com/google/common/hash/Hashing.java
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册