1. You must override hashCode in every class that overrides equals. Failure to do so will result in a violation of the general contract for Object.hashCode, which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.
2. The contract for Object.hashCode:
a) Whenever it is invoked on the same object more than once during an execution of an application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
b) If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
c) It is not required that if two objects are unequal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However producing distinct integer results for unequal objects may improve the performance of hash tables.
3. Even if the two instances happen to hash to the same bucket, the get method will almost certainly return null, as HashMap has an optimization that caches the hash code associated with each entry and doesn’t bother checking for object equality if the hash codes don’t match.
4. Ideally, a hash function should distribute any reasonable collection of unequal instances uniformly across all possible hash values. Here is a simple recipe:
a) Store some constant nonzero value, say, 17, in an int variable called result.
b) For each significant field f in your object (each field taken into account by the equals method, that is), do the following:
1) Compute an int hash code c for the field:
i) If the field is a boolean, compute (f ? 1 : 0).
ii) If the field is a byte, char, short, or int, compute (int) f.
iii) If the field is a long, compute (int) (f ^ (f >>> 32)).
iv) If the field is a float, compute Float.floatToIntBits(f).
v) If the field is a double, compute Double.doubleToLongBits(f), and then hash the resulting long as in iii)
vi) If the field is an object reference and this class’s equals method compares the field by recursively invoking equals, recursively invoke hashCode on the field. If a more complex comparison is required, compute a “canonical representation” for this field and invoke hashCode on the canonical representation. If the value of the field is null, return 0.
vii) If the field is an array, treat it as if each element were a separate field. If every element in an array field is significant, you can use one of the Arrays.hashCode methods.
2) Combine the hash code c computed in step 1) into result as follows:
result = 31 * result + c;
b) Return result.
5. You must exclude any fields that are not used in equals comparisons, or you risk violating the second provision of the hashCode contract. A nonzero initial value is used in step a) so the hash value will be affected by initial fields whose hash value, as computed in step 1), is zero. If zero were used as the initial value in step 1, the overall hash value would be unaffected by any such initial fields, which could increase collisions. The value 17 is arbitrary.
6. A nice property of 31 is that the multiplication can be replaced by a shift and a subtraction for better performance: 31 * i == (i << 5) - i. Modern VMs do this sort of optimization automatically.
7. If a class is immutable and the cost of computing the hash code is significant, you might consider caching the hash code in the object rather than recalculating it each time it is requested. If you believe that most objects of this type will be used as hash keys, then you should calculate the hash code when the instance is created. Otherwise, you might choose to lazily initialize it the first time hashCode is invoked.
- 浏览: 793579 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (651)
- Java (39)
- Java 初学者小问题 (66)
- 设计模式 (7)
- 项目管理 (3)
- 数据库 (1)
- 算法 (2)
- Java practices (6)
- Effective Java2读书笔记 (78)
- Linux (2)
- programming ruby 读书笔记 (5)
- Core Java Ninth Edition Volume I 读书笔记 (15)
- Pro Git 读书笔记 (12)
- Git (3)
- Maven in Action 读书笔记 (20)
- Web (12)
- 非技术类书籍 (11)
- 电影 (40)
- Web Cache (1)
- jquery (0)
- 历史 (4)
- Dive Into HTML5 读书笔记 (13)
- 三国演义小学毕业考 (79)
- 高效能人士的7个习惯 读书笔记 (12)
- Java Performance 读书笔记 (3)
- Protocol Buffer 学习笔记 (6)
- Mongo DB 学习笔记 (7)
- Morphia 学习笔记 (7)
- Algorithms -- Princeton 学习笔记 (13)
- String研究 (10)
- Hadoop: The Definitive Guide 读书笔记 (3)
- Java与模式读书笔记 (5)
- Date研究 (3)
- The Roman Empire 听课笔记 (4)
- Algorithms -- Standford 学习笔记 (16)
- Core Java Ninth Edition Volume II 读书笔记 (9)
- Thinking in Java 4th Edition 读书笔记 (21)
- Node : Up and Running 学习笔记 (5)
- Eloquent Javascript (8)
- Smashing Node.js 读书笔记 (1)
- Algorithms II -- Standford 学习笔记 (19)
- Algorithm II -- Princeton 学习笔记 (14)
- 网络安全 (2)
- Javascript (4)
- 正则表达式 (1)
- JAVA 7/8 (15)
- JVM (10)
- NodeJS (1)
- 鸟哥的linux私房菜读书笔记 (14)
- Web Service (1)
- The art of programming (9)
- Introduction to Algorithm 读书笔记 (4)
- Java 源码阅读 (0)
- Spring in Action 读书笔记 (2)
- Java Network Programming 读书笔记 (2)
最新评论
-
心存高远:
谢谢作者分享,刚好看到这里不太明白,现在茅塞顿开。不过runt ...
关于 Maven的传递依赖的理解 -
sxlkk:
851228082 写道甚至在某次技术会议现场遇到《Maven ...
关于 Maven的传递依赖的理解 -
851228082:
851228082 写道a----compile----b-- ...
第五章 坐标和依赖 -
851228082:
a----compile----b-----provided- ...
第五章 坐标和依赖 -
851228082:
甚至在某次技术会议现场遇到《Maven in action》的 ...
关于 Maven的传递依赖的理解
Item 9: Always override hashCode when you override equals
- 博客分类:
- Effective Java2读书笔记
发表评论
-
Item 78: Consider serialization proxies instead of serialized instances
2014-05-05 23:57 10961. The serialization proxy p ... -
Item 77: For instance control, prefer enum types to readResolve
2014-05-05 12:57 11671. The readResolve feature al ... -
Item 76: Write readObject methods defensively
2014-05-05 10:58 8621. The readObject method is e ... -
Item 75: Consider using a custom serialized form
2014-05-05 09:53 8981. Do not accept the default ... -
Item 74: Implement Serializable judiciously
2014-05-04 18:46 8661. A major cost of implementi ... -
Item 73: Avoid thread groups
2014-05-04 16:24 5901. Thread groups were origina ... -
Item 72: Don't depend on the thread scheduler
2014-05-04 16:14 7261. The best way to write a ro ... -
Item 71: Use lazy initialization judiciously
2014-05-04 15:47 8961. As is the case for most op ... -
Item 70: Document thread safety
2014-05-04 15:02 9691. In normal operation, Javad ... -
Item 69: Prefer concurrency utilities to wait and notify
2014-05-03 22:28 13441. Given the difficulty of us ... -
Item 68: Prefer executors and tasks to threads
2014-05-02 15:57 9261. java.util.concurrent conta ... -
Item 67: Avoid excessive synchronization
2014-05-02 11:46 5611. To avoid liveness and safe ... -
Item 66: Synchronize access to shared mutable data
2014-04-30 17:02 6761. Without synchronization, o ... -
Item 65: Don’t ignore exceptions
2014-04-24 15:29 6471. An empty catch block defea ... -
Item 64: Strive for failure atomicity
2014-04-24 15:12 8911. Generally speaking, a fail ... -
Item 63: Include failure-capture information in detail messages
2014-04-24 14:51 8781. When a program fails due t ... -
Item 62: Document all exceptions thrown by each method
2014-04-24 13:05 9251. Always declare checked ex ... -
Item 61: Throw exceptions appropriate to the abstraction
2014-04-23 17:55 6091. Higher layers should catch ... -
Item 60: Favor the use of standard exceptions
2014-04-22 10:33 6971. Reusing preexisting excep ... -
Item 59: Avoid unnecessary use of checked exceptions
2014-04-22 09:57 9211. Unlike return codes, the ...
相关推荐
Item 11: Always override hashCode when you override equals Item 12: Always override toString Item 13: Override clone judiciously Item 14: Consider implementing Comparable 4 Classes and Interfaces Item...
在Java编程语言中,`equals()` 和 `hashCode()` 方法是Object类中的两个核心方法,所有类都默认继承自Object类。这两个方法在处理对象比较和集合操作时起着至关重要的作用。当我们创建自定义类并需要对对象进行精确...
在Java编程语言中,`hashCode()` 和 `equals()` 是两个非常重要的方法,它们主要用于对象的比较和哈希表(如HashMap)的操作。标题提到的"HashCode相同equals不同的2位字符集合算法"涉及到的是一个特定场景:两个...
### hashCode和equals方法详解 #### 一、hashCode方法解析 在深入探讨`hashCode`方法之前,我们需要了解Java集合框架的基本概念。Java集合框架主要包括两大类集合:`List`和`Set`。 - **List**:这是一个有序集合...
在Java编程语言中,`equals()` 和 `hashCode()` 方法是对象的基本组成部分,它们在很多场景下都发挥着至关重要的作用。这两个方法与对象的相等性比较和哈希表(如HashMap、HashSet)的运作紧密相关。这篇博客将深入...
在Java编程语言中,`hashCode()` 和 `equals()` 方法是两个非常重要的概念,尤其是在处理对象比较和哈希表(如 `HashMap` 或 `HashSet`)时。这两个方法来源于 `Object` 类,是所有Java类的基类,因此,每个自定义类...
### Java中`hashCode()`与`equals()`方法详解 #### 前言 在Java编程语言中,`hashCode()`和`equals()`方法是非常重要的概念,它们不仅对于深入理解Java内存管理至关重要,也是实现自定义类的关键部分之一。本文将...
### set接口中hashCode和equals方法详解 #### 一、引言 在Java编程语言中,`Set`接口作为集合框架的重要组成部分,在实现无重复元素的数据结构方面扮演着关键角色。为了确保元素的唯一性,`Set`接口依赖于对象的`...
在Java编程语言中,`hashCode()`和`equals()`方法是对象身份验证的关键组成部分,它们主要用于对象的比较和哈希表(如HashMap、HashSet等)的操作。理解这两个方法的工作原理对于编写高效和可靠的代码至关重要。 ...
在Java编程中,`equals()` 和 `hashCode()` 方法是Object类中的两个重要方法,它们在处理对象相等性以及在哈希表(如HashSet、HashMap)中起到关键作用。当自定义类时,有时需要根据业务逻辑重写这两个方法以满足...
在Java编程语言中,`hashCode()` 和 `equals()` 方法对于对象的比较和处理至关重要,尤其在集合类(如Set和Map)中。这两个方法都源自`java.lang.Object`类,因此所有的Java类都默认继承了它们。理解并正确地重写这...
"关于Object中equals方法和hashCode方法判断的分析" 在 Java 中,Object 类提供了两个重要的方法:equals 方法和 hashCode 方法。这两个方法都是用于比较两个对象是否相等的,但它们的实现机理和作用域却有所不同。...
"Java重写equals及hashcode方法流程解析" Java中的equals和hashCode方法是两个非常重要的方法,它们都是Object类中的方法。在实际开发中,正确地重写这两个方法对于确保程序的正确性和性能至关重要。下面,我们将...
在Java编程语言中,`equals()`方法和`hashCode()`方法是两个非常重要的概念,它们主要用于对象的比较和哈希表的高效运作。本解析将深入探讨这两个方法的用途、实现原理以及它们之间的关联。 首先,`equals()`方法是...
详解hashCode()和equals()的本质区别和联系 在 Java 中,hashCode() 和 equals() 是两个非常重要的方法,它们都是从 Object 类继承来的。了解这两个方法的本质区别和联系对于 Java 开发者来说至关重要。 equals() ...
### Java中`hashCode()`与`equals()`详解 #### 一、引言 在Java编程中,`hashCode()`和`equals()`方法是两个非常重要的概念,它们对于理解和使用Java中的集合框架(尤其是`HashSet`和`HashMap`)至关重要。本文将...
### hashCode与equals方法详解 在Java编程语言中,`hashCode`和`equals`方法是非常重要的概念,它们在处理对象的比较、存储以及检索时扮演着关键角色。本文将深入探讨这两个方法之间的区别,并通过具体例子来说明...
在Java编程中,`equals()`和`hashCode()`方法是Object类中的两个重要方法。当我们创建自定义类并将其对象放入集合(如HashSet)时,往往需要重写这两个方法以确保集合能够正确地处理这些对象。IntelliJ IDEA,作为一...
第二种情况:对于采用哈希算法的集合,集合中对象必须重写 hashCode 方法,同时也要重写 equals 方法。这是因为哈希算法需要使用 hashCode 方法来计算对象的哈希值,而 equals 方法用于判断两个对象是否相等。 如何...
以下是一段示例代码,展示了如何使用`hashCode`方法和`equals`方法,并且解释了它们之间的关系: ```java public class Test { public static void main(String[] args) { HashSet<Demo1> set = new HashSet(); ...