源码解析为什么覆盖equals方法时总要覆盖hashCode方法

1 篇文章 0 订阅
1 篇文章 0 订阅
1、背景知识

本文代码基于jdk1.8分析,《Java编程思想》中有如下描述:

另外再看下Object.java对hashCode()方法的说明:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Returns a hash code value for the object. This method is 
  3.      * supported for the benefit of hash tables such as those provided by 
  4.      * {@link java.util.HashMap}. 
  5.      * <p> 
  6.      * The general contract of {@code hashCode} is: 
  7.      * <ul> 
  8.      * <li>Whenever it is invoked on the same object more than once during 
  9.      *     an execution of a Java application, the {@code hashCode} method 
  10.      *     must consistently return the same integer, provided no information 
  11.      *     used in {@code equals} comparisons on the object is modified. 
  12.      *     This integer need not remain consistent from one execution of an 
  13.      *     application to another execution of the same application. 
  14.      * <li>If two objects are equal according to the {@code equals(Object)} 
  15.      *     method, then calling the {@code hashCode} method on each of 
  16.      *     the two objects must produce the same integer result. 
  17.      * <li>It is <em>not</em> required that if two objects are unequal 
  18.      *     according to the {@link java.lang.Object#equals(java.lang.Object)} 
  19.      *     method, then calling the {@code hashCode} method on each of the 
  20.      *     two objects must produce distinct integer results.  However, the 
  21.      *     programmer should be aware that producing distinct integer results 
  22.      *     for unequal objects may improve the performance of hash tables. 
  23.      * </ul> 
  24.      * <p> 
  25.      * As much as is reasonably practical, the hashCode method defined by 
  26.      * class {@code Object} does return distinct integers for distinct 
  27.      * objects. (This is typically implemented by converting the internal 
  28.      * address of the object into an integer, but this implementation 
  29.      * technique is not required by the 
  30.      * Java™ programming language.) 
  31.      * 
  32.      * @return  a hash code value for this object. 
  33.      * @see     java.lang.Object#equals(java.lang.Object) 
  34.      * @see     java.lang.System#identityHashCode 
  35.      */  
  36.     public native int hashCode();  

对于3点约定翻译如下:

1)在java应用执行期间,只要对象的equals方法的比较操作所用到的信息没有被修改,那么对这同一对象调用多次hashCode方法都必须始终如一地同一个整数。在同一个应用程序的多次执行过程中,每次执行该方法返回的整数可以不一致。

2)如果两个对象根据equals(Object)方法比较是相等的,那么调用这两个对象中任意一个对象的hashCode方法都必须产生同样的整数结果。

3)如果两个对象根据equals(Object)方法比较是不相等的,那么调用这两个对象中任意一个对象的hashCode方法没必要产生不同的整数结果。但是程序猿应该知道,给不同的对象产生截然不同的整数结果,有可能提高散列表(hash table)的性能。


因此,覆盖equals时总是要覆盖hashCode是一种通用的约定,而不是必须的,如果和基于散列的集合(HashMap、HashSet、HashTable)一起工作时,特别是将该对象作为key值的时候,一定要覆盖hashCode,否则会出现错误。那么既然是一种规范,那么作为程序猿的我们就有必要必须执行,以免出现问题。

下面就以HashMap为例分析其必要性

2、HashMap内部实现

常用形式如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class PhoneNumber {  
  2.     private int areaCode;  
  3.     private int prefix;  
  4.     private int lineNumber;  
  5.   
  6.     public PhoneNumber(int areaCode, int prefix, int lineNumber) {  
  7.         this.areaCode = areaCode;  
  8.         this.prefix = prefix;  
  9.         this.lineNumber = lineNumber;  
  10.     }  
  11.       
  12.     @Override  
  13.     public boolean equals(Object o) {  
  14.         if (this == o) return true;  
  15.         if (o == null || getClass() != o.getClass()) return false;  
  16.   
  17.         PhoneNumber that = (PhoneNumber) o;  
  18.   
  19.         if (areaCode != that.areaCode) return false;  
  20.         if (prefix != that.prefix) return false;  
  21.         return lineNumber == that.lineNumber;  
  22.     }  
  23.   
  24.     @Override  
  25.     public int hashCode() {  
  26.         int result = areaCode;  
  27.         result = 31 * result + prefix;  
  28.         result = 31 * result + lineNumber;  
  29.         return result;  
  30.     }  
  31.   
  32.     public static void main(String[] args){  
  33.         Map<PhoneNumber,String> phoneNumberStringMap = new HashMap<PhoneNumber,String>();  1)初始化  
  34.         phoneNumberStringMap.put(new PhoneNumber(1234567890), "honghailiang");         2)put存储  
  35.         System.out.println(phoneNumberStringMap.get(new PhoneNumber(1234567890)));     3)get获取  
  36.   
  37.     }  
  38. }  
1)初始化
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Constructs an empty <tt>HashMap</tt> with the default initial capacity 
  3.      * (16) and the default load factor (0.75). 
  4.      */  
  5.     public HashMap() {  
  6.         this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted  
  7.     }  

创建一个具有默认负载因子的HashMap,默认负载因子是0.75

2)put存储

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Associates the specified value with the specified key in this map. 
  3.      * If the map previously contained a mapping for the key, the old 
  4.      * value is replaced. 
  5.      * 
  6.      * @param key key with which the specified value is to be associated 
  7.      * @param value value to be associated with the specified key 
  8.      * @return the previous value associated with <tt>key</tt>, or 
  9.      *         <tt>null</tt> if there was no mapping for <tt>key</tt>. 
  10.      *         (A <tt>null</tt> return can also indicate that the map 
  11.      *         previously associated <tt>null</tt> with <tt>key</tt>.) 
  12.      */  
  13.     public V put(K key, V value) {  
  14.         return putVal(hash(key), key, value, falsetrue);  
  15.     }  

通过注释可以看出,key值相同的情况下,会将前者覆盖,也就是HashMap中不允许存在重复的Key值。并且该方法是有返回值的,返回key值的上一个value,如果之前没有map则返回null。继续看putVal

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Implements Map.put and related methods 
  3.      * 
  4.      * @param hash hash for key 
  5.      * @param key the key 
  6.      * @param value the value to put 
  7.      * @param onlyIfAbsent if true, don't change existing value 
  8.      * @param evict if false, the table is in creation mode. 
  9.      * @return previous value, or null if none 
  10.      */  
  11.     final V putVal(int hash, K key, V value, boolean onlyIfAbsent,  
  12.                    boolean evict) {  
  13.         Node<K,V>[] tab; Node<K,V> p; int n, i;  
  14.         if ((tab = table) == null || (n = tab.length) == 0)      //tab为空则创建  
  15.             n = (tab = resize()).length;  
  16.         if ((p = tab[i = (n - 1) & hash]) == null)               //根据下标获取,如果没有(没发生碰撞(hash值相同))则直接创建  
  17.             tab[i] = newNode(hash, key, value, null);  
  18.         else {                                                   //如果发生了碰撞进行如下处理  
  19.             Node<K,V> e; K k;  
  20.             if (p.hash == hash &&  
  21.                 ((k = p.key) == key || (key != null && key.equals(k))))  
  22.                 e = p;  
  23.             else if (p instanceof TreeNode)                      //为红黑数的情况  
  24.                 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);  
  25.             else {                                               //为链表的情况,普通Node  
  26.                 for (int binCount = 0; ; ++binCount) {  
  27.                     if ((e = p.next) == null) {  
  28.                         p.next = newNode(hash, key, value, null); //链表保存  
  29.                         if (binCount >= TREEIFY_THRESHOLD - 1// -1 for 1st  
  30.                             treeifyBin(tab, hash);                //如果链表长度超过了8则转为红黑树   
  31.                         break;  
  32.                     }  
  33.                     if (e.hash == hash &&  
  34.                         ((k = e.key) == key || (key != null && key.equals(k))))  
  35.                         break;  
  36.                     p = e;  
  37.                 }  
  38.             }  
  39.             if (e != null) { // existing mapping for key                     <span class="comment">// 写入,并返回oldValue</span>  
  40.                 V oldValue = e.value;  
  41.                 if (!onlyIfAbsent || oldValue == null)  
  42.                     e.value = value;  
  43.                 afterNodeAccess(e);  
  44.                 return oldValue;  
  45.             }  
  46.         }  
  47.         ++modCount;  
  48.         if (++size > threshold)          <span class="comment">// 超过load factor*current capacity,resize</span>  
  49.             resize();  
  50.         afterNodeInsertion(evict);  
  51.         return null;  
  52.     }  


可以看到第一个参数时key的hash,如下

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Computes key.hashCode() and spreads (XORs) higher bits of hash 
  3.      * to lower.  Because the table uses power-of-two masking, sets of 
  4.      * hashes that vary only in bits above the current mask will 
  5.      * always collide. (Among known examples are sets of Float keys 
  6.      * holding consecutive whole numbers in small tables.)  So we 
  7.      * apply a transform that spreads the impact of higher bits 
  8.      * downward. There is a tradeoff between speed, utility, and 
  9.      * quality of bit-spreading. Because many common sets of hashes 
  10.      * are already reasonably distributed (so don't benefit from 
  11.      * spreading), and because we use trees to handle large sets of 
  12.      * collisions in bins, we just XOR some shifted bits in the 
  13.      * cheapest possible way to reduce systematic lossage, as well as 
  14.      * to incorporate impact of the highest bits that would otherwise 
  15.      * never be used in index calculations because of table bounds. 
  16.      */  
  17.     static final int hash(Object key) {  
  18.         int h;  
  19.         return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);  
  20.     }  

综合考虑了速度、作用、质量因素,就是把key的hashCode的高16bit和低16bit异或了一下。因为现在大多数的hashCode的分布已经很不错了,就算是发生了碰撞也用O(logn)的tree去做了。仅仅异或一下,既减少了系统的开销,也不会造成的因为高位没有参与下标的计算(table长度比较小时),从而引起的碰撞。再回过头来看putVal

1.先判断存有Node数组table是否为null或者大小为0,如果是初始化一个tab并获取它的长度。resize()后面再说,先看下Node的结构

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Basic hash bin node, used for most entries.  (See below for 
  3.      * TreeNode subclass, and in LinkedHashMap for its Entry subclass.) 
  4.      */  
  5.     static class Node<K,V> implements Map.Entry<K,V> {  
  6.         final int hash;  
  7.         final K key;  
  8.         V value;  
  9.         Node<K,V> next;  
  10.   
  11.         Node(int hash, K key, V value, Node<K,V> next) {  
  12.             this.hash = hash;  
  13.             this.key = key;  
  14.             this.value = value;  
  15.             this.next = next;  
  16.         }  
  17.   
  18.         public final K getKey()        { return key; }  
  19.         public final V getValue()      { return value; }  
  20.         public final String toString() { return key + "=" + value; }  
  21.   
  22.         public final int hashCode() {  
  23.             return Objects.hashCode(key) ^ Objects.hashCode(value);  
  24.         }  
  25.   
  26.         public final V setValue(V newValue) {  
  27.             V oldValue = value;  
  28.             value = newValue;  
  29.             return oldValue;  
  30.         }  
  31.   
  32.         public final boolean equals(Object o) {  
  33.             if (o == this)  
  34.                 return true;  
  35.             if (o instanceof Map.Entry) {  
  36.                 Map.Entry<?,?> e = (Map.Entry<?,?>)o;  
  37.                 if (Objects.equals(key, e.getKey()) &&  
  38.                     Objects.equals(value, e.getValue()))  
  39.                     return true;  
  40.             }  
  41.             return false;  
  42.         }  
  43.     }  


Node实现了链表形式,用于存储hash值没有发生碰撞的hash、key、value,如果发生碰撞则用TreeNode存储,继承自Entry,并最终继承自Node

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Entry for Tree bins. Extends LinkedHashMap.Entry (which in turn 
  3.      * extends Node) so can be used as extension of either regular or 
  4.      * linked node. 
  5.      */  
  6.     static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {  
  7.         TreeNode<K,V> parent;  // red-black tree links  
  8.         TreeNode<K,V> left;  
  9.         TreeNode<K,V> right;  
  10.         TreeNode<K,V> prev;    // needed to unlink next upon deletion  
  11.         boolean red;  
  12.         TreeNode(int hash, K key, V val, Node<K,V> next) {  
  13.             super(hash, key, val, next);  
  14.         }  
  15. ......  
  16. }  


2.以(n - 1) & hash为下标从tab中取出Node,如果不存在,则以hash、Key、value、null为参数new一个Node,存储到以(n - 1) & hash为下标的tab中

3.如果该下标中有值,也就是Node存在。如果为TreeNode,就用putTreeVal进行树节点的存储。否则以链表的形式存储,如果链表长度超过8则转为红黑树存储。

4.如果节点已经存在就替换old value(保证key的唯一性)

5.如果bucket(Node数组)满了(超过load factor*current capacity),就要resize。

总结:put存储过程:将K/V传给put方法时,它调用hashCode计算hash从而得到Node位置,进一步存储,HashMap会根据当前Node的占用情况自动调整容量(超过Load Facotr则resize为原来的2倍)。可见如果不覆盖hashCode就不能正确的存储。


3)get获取
看完put,再看下get
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Returns the value to which the specified key is mapped, 
  3.      * or {@code null} if this map contains no mapping for the key. 
  4.      * 
  5.      * <p>More formally, if this map contains a mapping from a key 
  6.      * {@code k} to a value {@code v} such that {@code (key==null ? k==null : 
  7.      * key.equals(k))}, then this method returns {@code v}; otherwise 
  8.      * it returns {@code null}.  (There can be at most one such mapping.) 
  9.      * 
  10.      * <p>A return value of {@code null} does not <i>necessarily</i> 
  11.      * indicate that the map contains no mapping for the key; it's also 
  12.      * possible that the map explicitly maps the key to {@code null}. 
  13.      * The {@link #containsKey containsKey} operation may be used to 
  14.      * distinguish these two cases. 
  15.      * 
  16.      * @see #put(Object, Object) 
  17.      */  
  18.     public V get(Object key) {  
  19.         Node<K,V> e;  
  20.         return (e = getNode(hash(key), key)) == null ? null : e.value;  
  21.     }  

get方法又用到了hash(),是根据key的hash和key获取Node,返回的值就是Node的value属性。下面主要看下getNode方法即可
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Implements Map.get and related methods 
  3.      * 
  4.      * @param hash hash for key 
  5.      * @param key the key 
  6.      * @return the node, or null if none 
  7.      */  
  8.     final Node<K,V> getNode(int hash, Object key) {  
  9.         Node<K,V>[] tab; Node<K,V> first, e; int n; K k;  
  10.         if ((tab = table) != null && (n = tab.length) > 0 &&  
  11.             (first = tab[(n - 1) & hash]) != null) {                    //map中存在的情况,不存在则直接返回null  
  12.             if (first.hash == hash && // always check first node  
  13.                 ((k = first.key) == key || (key != null && key.equals(k))))     //第一个直接命中  
  14.                 return first;  
  15.             if ((e = first.next) != null) {                             //如果第一个没命中,获取下一个节点  
  16.                 if (first instanceof TreeNode)  
  17.                     return ((TreeNode<K,V>)first).getTreeNode(hash, key);   //如果下一个节点是TreeNode,则用getTreeNode当时获取  
  18.                 do {  
  19.                     if (e.hash == hash &&  
  20.                         ((k = e.key) == key || (key != null && key.equals(k))))    //循环节点链表,直到命中  
  21.                         return e;  
  22.                 } while ((e = e.next) != null);  
  23.             }  
  24.         }  
  25.         return null;  
  26.     }  

1)第一个直接命中
2)否则,获取下一个节点,如果是红黑树,则从红黑树中获取,否则循环节点链表,直至命中。命中的条件是hash相等且key也相同(基本类型==,自定义类则用equals)。

总结:获取对象时,我们将K传给get,它调用hashCode计算hash从而得到Node位置,并进一步调用==或equals()方法确定键值对。可见为了正确的获取,要覆盖hashCode和equals方法

题外话:当链表长度超过8的时候,java8用红黑树代替了链表,目的是提高性能,这里不展开。HashMap是基于Map接口的实现,存储键值对时,它可以接收null的键值,是非同步的,HashMap存储着Entry(hash, key, value, next)对象。

3、为什么覆盖equals的时候要覆盖hashCode
通过HashMap的实现原理,可以看出当自定义类作为key值存在的时候一定要这样做,但不作为key值可以选择不这样做(但为了规范起见,还是要覆盖,因此就变成了必须的了)。如果将测试代码中的equals或hashCode注释掉都不能得到正确的结果:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class PhoneNumber {  
  2.     private int areaCode;  
  3.     private int prefix;  
  4.     private int lineNumber;  
  5.   
  6.     public PhoneNumber(int areaCode, int prefix, int lineNumber) {  
  7.         this.areaCode = areaCode;  
  8.         this.prefix = prefix;  
  9.         this.lineNumber = lineNumber;  
  10.     }  
  11.   
  12. //    @Override  
  13. //    public boolean equals(Object o) {  
  14. //        if (this == o) return true;  
  15. //        if (o == null || getClass() != o.getClass()) return false;  
  16. //  
  17. //        PhoneNumber that = (PhoneNumber) o;  
  18. //  
  19. //        if (areaCode != that.areaCode) return false;  
  20. //        if (prefix != that.prefix) return false;  
  21. //        return lineNumber == that.lineNumber;  
  22. //    }  
  23.   
  24.     @Override  
  25.     public int hashCode() {  
  26.         int result = areaCode;  
  27.         result = 31 * result + prefix;  
  28.         result = 31 * result + lineNumber;  
  29.         return result;  
  30.     }  
  31.   
  32.     public static void main(String[] args){  
  33.         Map<PhoneNumber,String> phoneNumberStringMap = new HashMap<PhoneNumber,String>();  
  34.         phoneNumberStringMap.put(new PhoneNumber(1234567890), "honghailiang");  
  35.         System.out.println(phoneNumberStringMap.get(new PhoneNumber(1234567890)));  
  36.     }  
  37. }  
上述结果均为null;

题外话Java中的基本类型可以作为key值,包括String类,String类已经覆盖了equals方法和hashCode方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值