`
leonzhx
  • 浏览: 786008 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 9: Always override hashCode when you override equals

阅读更多

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.

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

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

    重写equals和hashcode方法_equals_重写equals和hashcode方法_

    在Java编程语言中,`equals()` 和 `hashCode()` 方法是Object类中的两个核心方法,所有类都默认继承自Object类。这两个方法在处理对象比较和集合操作时起着至关重要的作用。当我们创建自定义类并需要对对象进行精确...

    HashCode相同equals不同的2位字符集合算法

    在Java编程语言中,`hashCode()` 和 `equals()` 是两个非常重要的方法,它们主要用于对象的比较和哈希表(如HashMap)的操作。标题提到的"HashCode相同equals不同的2位字符集合算法"涉及到的是一个特定场景:两个...

    hashcode和equals的分析

    ### hashCode和equals方法详解 #### 一、hashCode方法解析 在深入探讨`hashCode`方法之前,我们需要了解Java集合框架的基本概念。Java集合框架主要包括两大类集合:`List`和`Set`。 - **List**:这是一个有序集合...

    Java_重写equals()和hashCode()

    在Java编程语言中,`equals()` 和 `hashCode()` 方法是对象的基本组成部分,它们在很多场景下都发挥着至关重要的作用。这两个方法与对象的相等性比较和哈希表(如HashMap、HashSet)的运作紧密相关。这篇博客将深入...

    hashcode()和equals()

    在Java编程语言中,`hashCode()` 和 `equals()` 方法是两个非常重要的概念,尤其是在处理对象比较和哈希表(如 `HashMap` 或 `HashSet`)时。这两个方法来源于 `Object` 类,是所有Java类的基类,因此,每个自定义类...

    java中hashcode()和equals()方法详解

    ### Java中`hashCode()`与`equals()`方法详解 #### 前言 在Java编程语言中,`hashCode()`和`equals()`方法是非常重要的概念,它们不仅对于深入理解Java内存管理至关重要,也是实现自定义类的关键部分之一。本文将...

    set接口经常用的hashCode和equals方法详解

    ### set接口中hashCode和equals方法详解 #### 一、引言 在Java编程语言中,`Set`接口作为集合框架的重要组成部分,在实现无重复元素的数据结构方面扮演着关键角色。为了确保元素的唯一性,`Set`接口依赖于对象的`...

    java中hashcode()和equals()的详解

    在Java编程语言中,`hashCode()`和`equals()`方法是对象身份验证的关键组成部分,它们主要用于对象的比较和哈希表(如HashMap、HashSet等)的操作。理解这两个方法的工作原理对于编写高效和可靠的代码至关重要。 ...

    重写hashCode()和equals()方法详细介绍

    在Java编程中,`equals()` 和 `hashCode()` 方法是Object类中的两个重要方法,它们在处理对象相等性以及在哈希表(如HashSet、HashMap)中起到关键作用。当自定义类时,有时需要根据业务逻辑重写这两个方法以满足...

    java中hashCode、equals的使用方法教程

    在Java编程语言中,`hashCode()` 和 `equals()` 方法对于对象的比较和处理至关重要,尤其在集合类(如Set和Map)中。这两个方法都源自`java.lang.Object`类,因此所有的Java类都默认继承了它们。理解并正确地重写这...

    关于Object中equals方法和hashCode方法判断的分析

    "关于Object中equals方法和hashCode方法判断的分析" 在 Java 中,Object 类提供了两个重要的方法:equals 方法和 hashCode 方法。这两个方法都是用于比较两个对象是否相等的,但它们的实现机理和作用域却有所不同。...

    Java重写equals及hashcode方法流程解析

    "Java重写equals及hashcode方法流程解析" Java中的equals和hashCode方法是两个非常重要的方法,它们都是Object类中的方法。在实际开发中,正确地重写这两个方法对于确保程序的正确性和性能至关重要。下面,我们将...

    Java equals 方法与hashcode 方法的深入解析.rar

    在Java编程语言中,`equals()`方法和`hashCode()`方法是两个非常重要的概念,它们主要用于对象的比较和哈希表的高效运作。本解析将深入探讨这两个方法的用途、实现原理以及它们之间的关联。 首先,`equals()`方法是...

    详解hashCode()和equals()的本质区别和联系

    详解hashCode()和equals()的本质区别和联系 在 Java 中,hashCode() 和 equals() 是两个非常重要的方法,它们都是从 Object 类继承来的。了解这两个方法的本质区别和联系对于 Java 开发者来说至关重要。 equals() ...

    java中hashcode()和equals()的详解.docx

    ### Java中`hashCode()`与`equals()`详解 #### 一、引言 在Java编程中,`hashCode()`和`equals()`方法是两个非常重要的概念,它们对于理解和使用Java中的集合框架(尤其是`HashSet`和`HashMap`)至关重要。本文将...

    hashcode与eqault比较

    ### hashCode与equals方法详解 在Java编程语言中,`hashCode`和`equals`方法是非常重要的概念,它们在处理对象的比较、存储以及检索时扮演着关键角色。本文将深入探讨这两个方法之间的区别,并通过具体例子来说明...

    重写equals方法

    第二种情况:对于采用哈希算法的集合,集合中对象必须重写 hashCode 方法,同时也要重写 equals 方法。这是因为哈希算法需要使用 hashCode 方法来计算对象的哈希值,而 equals 方法用于判断两个对象是否相等。 如何...

    如何在IDEA中对 hashCode()和 equals() 利用快捷键快速进行方法重写

    在Java编程中,`equals()`和`hashCode()`方法是Object类中的两个重要方法。当我们创建自定义类并将其对象放入集合(如HashSet)时,往往需要重写这两个方法以确保集合能够正确地处理这些对象。IntelliJ IDEA,作为一...

    hashCode的作用

    以下是一段示例代码,展示了如何使用`hashCode`方法和`equals`方法,并且解释了它们之间的关系: ```java public class Test { public static void main(String[] args) { HashSet&lt;Demo1&gt; set = new HashSet(); ...

Global site tag (gtag.js) - Google Analytics