首页 > 代码库 > Why we should overwrite the hashCode() when we overwrite the equals()

Why we should overwrite the hashCode() when we overwrite the equals()

Preface

  Though I have used Java programme language for almost a year, I‘m not familiar with a notion ‘hash‘. I have gotten a little knowledge about hash recently because I am reading a book of algorithm, which introduce the hash function and so on. So I follow the example in the book to write a sample hash routine. During the process, I‘m baffling what is hashCode of the Java. As a consequence, I turn to the omnipotent internet. I will share my acquisition. In the following Articles, I will summarize some knowledge of hashCode in the java. In this article, I will concenterate on the topic why we need overwrite the hashCode() when we overwrite the equals()

 

What is the feature of equals()

  In the java, each object have its own equals function. The function is defined in its parent java.lang.Object. when we want to compare two object if they are ‘the same‘, we prefer to use it. I think many programmers have ever used it, and must be familiar with its features.

 

What is hashCode()

  It is by design useful for only one thing: putting an object in a hash table. Hence the name. As we all known, we put an object into a hash table accroding the hash code. The code is a identity of a object in the hash table. when we want to achieve the object from the table, we should provide the hash code of the object. Then we can calculate the position of the object in the table. I will not introduce much knowledge about the mechanism of a hash function.

 

Why we should overwrite the hashCode() when we overwrite the equals()

 In the java, the hashCode() function is closely associated with HashMap and HashSet. From the source code of the two collections, we can figure out the relationship. From the put method and the get method of HashMap,

 1  public V put(K key, V value)  2      {  3          //if the key is null, invoke the putForNullKey function 4          if (key == null)  5              return putForNullKey(value);  6          //derive the key‘s hashCode and invoke the hash method to calculate its hash value  7          int hash = hash(key.hashCode());  8          //search the index of the hash value in the hash table 9           int i = indexFor(hash, table.length);10           //if the Entry of the ‘i‘ index is not null, loop the e 11          for (Entry<K,V> e = table[i]; e != null; e = e.next) 12          { 13              Object k; 14              //if find a key and its hash value is the same with the target object‘s hash value and the key is equals the target object,15              //return the key‘s value16              if (e.hash == hash && ((k = e.key) == key 17                  || key.equals(k))) 18              { 19                  V oldValue =http://www.mamicode.com/ e.value; 20                  e.value =http://www.mamicode.com/ value; 21                  e.recordAccess(this); 22                  return oldValue; 23              } 24          } 25          //if the Entry of the ‘i‘ index is  null,26          modCount++; 27          //add the target key and its value to the table28          addEntry(hash, key, value, i); 29          return null; 30      } 
  public V get(Object key) {            if (key == null)                return getForNullKey();            int hash = hash(key.hashCode());            for (Entry<K,V> e = table[indexFor(hash, table.length)];                 e != null;                 e = e.next) {                Object k;                if (e.hash == hash && ((k = e.key) == key || key.equals(k)))                    return e.value;            }            return null;        }

From the code above, it is obvious that when we employ a HashMap or HashSet and use their put and get, the collection will compare both of the objects‘s hash value .

So the key point of overwriting the hashCode() is that the same hashCode should be returned no matter when we invoke the hashCode() of a object.

 

If the hashCode() does not return the same hash value, the ‘put‘ and the ‘get‘ method will not get the same hash value, which will lead to a terrible result that we can not get the desired object. As a conquence, if we use the equals and HashMap or HashSet, we should take care of the hashCode(). That is when we overwrite the equals(), we need overwrite the hashCode().

  To explain the reason in deatil, I prefer to quote a example from a bloger named ‘fhuan123‘ to illustrate my point.

       我们看一个简单的例子,就能更加清楚的理解上面的意思。假定我们写了一个类:Person (人),我们判断一个对象“人”是否指向同一个人,只要知道这个人的身份证号一直就可以了。

      先来个没有重写Code类的hashcode()的例子吧,看看是什么效果:

  1 package com.fit;  2   3 import java.util.HashMap;  4   5 /**  6  * 身份证类  7  *   8  * @author ZYD  9  *  10  */ 11 public class Code { 12  13     /** 14      * 身份证号码,一旦确定就不能更改 15      */ 16     private final int id; 17  18     public int getId() { 19         return id; 20     } 21  22     /** 23      * 通过构造方法确定身份证号码 24      *  25      * @param id 26      */ 27     public Code(int id) { 28         this.id = id; 29     } 30  31     /** 32      * 重写equals()方法 33      */ 34     public boolean equals(Object o) { 35         // 如果地址一样,则两个对象相同 36         if (this == o) { 37             return true; 38         } 39         // 如果两个对象是同一类型,则比较其属性值是否都相同。如果都相同,则说明两个对象也相同;否则,说明这两个对象不相同。 40         if (o instanceof Code) { 41             Code co = (Code) o; 42             boolean b = (co.id == this.id); 43             return b; 44         } 45         return false; 46     } 47  48     /** 49      * 重写toString()方法 50      */ 51     public String toString() { 52         return "【身份证】:" + id; 53     } 54      55     /** 56      * 测试 57      * @param args 58      */ 59     public static void main(String[] args) { 60          61          HashMap<Code, Person> map = new HashMap<Code, Person>(); 62           63          Person p1 = new Person(new Code(10001),"张三"); 64          Person p2 = new Person(new Code(10002),"李四"); 65           66          map.put(p1.getCode(), p1); 67          map.put(p2.getCode(), p2); 68           69          System.out.println("HashMap 中存放的人员信息:\n"+map); 70           71          //张三改名为张山,身份证号不变。 72          Person p3 = new Person(new Code(10001),"张山"); 73          map.put(p3.getCode(), p3); 74           75          System.out.println("张三改名为张山后 HashMap 中存放的人员信息:\n"+map); 76           77          //查找身份证为10001 的人员信息 78          System.out.println("查找身份证为:10001 的人员信息:"+map.get(new Code(10001))); 79     } 80 } 81  82 /** 83  * 人类 84  * @author Administrator 85  * 86  */ 87 class Person { 88  89     /** 90      * 每一个成人都有一个身份证 91      */ 92     private Code code; 93  94     /** 95      * 姓名 96      */ 97     private String name; 98  99     public Code getCode() {100         return code;101     }102 103     public void setCode(Code code) {104         this.code = code;105     }106 107     public String getName() {108         return name;109     }110 111     public void setName(String name) {112         this.name = name;113     }114 115     public Person() {116 117     }118 119     public Person(Code code, String name) {120         this.code = code;121         this.name = name;122     }123 124     /**125      * 重写equals()方法 当两个人得身份证号相同以及姓名相同时,表示这两个人是同一个人。126      */127     public boolean equals(Object o) {128         if (o == this) {129             return true;130         }131         if (o instanceof Person) {132             Person p = (Person) o;133             boolean b = this.code.equals(p.code) && this.name.equals(p.name);134             return b;135         }136         return false;137     }138 139     /**140      * 重写toString()方法141      */142     public String toString() {143         return "【姓名】:" + name + "  ";144     }145 }

 

  

运行结果:

 

HashMap 中存放的人员信息:
{【身份证】:10002=【姓名】:李四  , 【身份证】:10001=【姓名】:张三  }
张三改名为张山后 HashMap 中存放的人员信息:
{【身份证】:10002=【姓名】:李四  , 【身份证】:10001=【姓名】:张三  , 【身份证】:10001=【姓名】:张山  }
查找身份证为:10001 的人员信息:null

 

从上面的结果可以看出:

 

我们所做的更新和查找操作都失败了。失败的原因就是我们的身份证类: Code 没有覆写 hashCode() 方法。这个时候,当查找一样的身份证号码的键值对的时候,使用的是默认的对象的内存地址来进行定位。这样,后面的所有的身份证号对象

new Code(10001) 产生的 hashCode () 值都是不一样的,所以导致操作失败。

 

 重写Code类的hashcode(),代码上:

  1 package com.fit;  2   3 import java.util.HashMap;  4   5 /**  6  * 身份证类  7  *   8  * @author ZYD  9  *  10  */ 11 public class Code { 12  13     /** 14      * 身份证号码,一旦确定就不能更改 15      */ 16     private final int id; 17  18     public int getId() { 19         return id; 20     } 21  22     /** 23      * 通过构造方法确定身份证号码 24      *  25      * @param id 26      */ 27     public Code(int id) { 28         this.id = id; 29     } 30  31     /** 32      * 重写equals()方法 33      */ 34     public boolean equals(Object o) { 35         // 如果地址一样,则两个对象相同 36         if (this == o) { 37             return true; 38         } 39         // 如果两个对象是同一类型,则比较其属性值是否都相同。如果都相同,则说明两个对象也相同;否则,说明这两个对象不相同。 40         if (o instanceof Code) { 41             Code co = (Code) o; 42             boolean b = (co.id == this.id); 43             return b; 44         } 45         return false; 46     } 47  48     /** 49      * 重写hashcode()方法,以身份证号码作为hash码。 50      *  51      * @return 52      */ 53     public int hashCode() { 54         return id; 55     } 56  57     /** 58      * 重写toString()方法 59      */ 60     public String toString() { 61         return "【身份证】:" + id; 62     } 63      64     /** 65      * 测试 66      * @param args 67      */ 68     public static void main(String[] args) { 69          70          HashMap<Code, Person> map = new HashMap<Code, Person>(); 71           72          Person p1 = new Person(new Code(10001),"张三"); 73          Person p2 = new Person(new Code(10002),"李四"); 74           75          map.put(p1.getCode(), p1); 76          map.put(p2.getCode(), p2); 77           78          System.out.println("HashMap 中存放的人员信息:\n"+map); 79           80          //张三改名为张山,身份证号不变。 81          Person p3 = new Person(new Code(10001),"张山"); 82          map.put(p3.getCode(), p3); 83           84          System.out.println("张三改名为张山后 HashMap 中存放的人员信息:\n"+map); 85           86          //查找身份证为10001 的人员信息 87          System.out.println("查找身份证为:10001 的人员信息:"+map.get(new Code(10001))); 88     } 89 } 90  91 /** 92  * 人类 93  * @author Administrator 94  * 95  */ 96 class Person { 97  98     /** 99      * 每一个成人都有一个身份证100      */101     private Code code;102 103     /**104      * 姓名105      */106     private String name;107 108     public Code getCode() {109         return code;110     }111 112     public void setCode(Code code) {113         this.code = code;114     }115 116     public String getName() {117         return name;118     }119 120     public void setName(String name) {121         this.name = name;122     }123 124     public Person() {125 126     }127 128     public Person(Code code, String name) {129         this.code = code;130         this.name = name;131     }132 133     /**134      * 重写equals()方法 当两个人得身份证号相同以及姓名相同时,表示这两个人是同一个人。135      */136     public boolean equals(Object o) {137         if (o == this) {138             return true;139         }140         if (o instanceof Person) {141             Person p = (Person) o;142             boolean b = this.code.equals(p.code) && this.name.equals(p.name);143             return b;144         }145         return false;146     }147 148     /**149      * 重写toString()方法150      */151     public String toString() {152         return "【姓名】:" + name + "  ";153     }154 }

运行效果:

 

HashMap 中存放的人员信息:
{【身份证】:10001=【姓名】:张三  , 【身份证】:10002=【姓名】:李四  }
张三改名为张山后 HashMap 中存放的人员信息:
{【身份证】:10001=【姓名】:张山  , 【身份证】:10002=【姓名】:李四  }
查找身份证为:10001 的人员信息:【姓名】:张山  

 

Summary

  if we will put the target class into a colleciton of hash, such as HashMap or HashSet, we would better to overwrite the hashCode() method. As a result of overwriting , if a object equals the other, they should have the same hash value.

 

  In the next chapter, I will summary some rules and guidlines when overwrite a hashCode() method.

 

References

  Guidelines and rules for GetHashCode

  java中HashMap详解

  JAVA中重写equals()方法为什么要重写hashcode()方法说明 

  重写equal 的同时为什么必须重写hashcode?

  解析Java对象的equals()和hashCode()的使用

  

 

Why we should overwrite the hashCode() when we overwrite the equals()