1、背景知识

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

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

/**
     * Returns a hash code value for the object. This method is
     * supported for the benefit of hash tables such as those provided by
     * {@link java.util.HashMap}.
     * <p>
     * The general contract of {@code hashCode} is:
     * <ul>
     * <li>Whenever it is invoked on the same object more than once during
     *     an execution of a Java application, the {@code hashCode} method
     *     must consistently return the same integer, provided no information
     *     used in {@code equals} comparisons on the object is modified.
     *     This integer need not remain consistent from one execution of an
     *     application to another execution of the same application.
     * <li>If two objects are equal according to the {@code equals(Object)}
     *     method, then calling the {@code hashCode} method on each of
     *     the two objects must produce the same integer result.
     * <li>It is <em>not</em> required that if two objects are unequal
     *     according to the {@link java.lang.Object#equals(java.lang.Object)}
     *     method, then calling the {@code hashCode} method on each of the
     *     two objects must produce distinct integer results.  However, the
     *     programmer should be aware that producing distinct integer results
     *     for unequal objects may improve the performance of hash tables.
     * </ul>
     * <p>
     * As much as is reasonably practical, the hashCode method defined by
     * class {@code Object} does return distinct integers for distinct
     * objects. (This is typically implemented by converting the internal
     * address of the object into an integer, but this implementation
     * technique is not required by the
     * Java™ programming language.)
     *
     * @return  a hash code value for this object.
     * @see     java.lang.Object#equals(java.lang.Object)
     * @see     java.lang.System#identityHashCode
     */
    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内部实现

常用形式如下:

public class PhoneNumber {
    private int areaCode;
    private int prefix;
    private int lineNumber;

    public PhoneNumber(int areaCode, int prefix, int lineNumber) {
        this.areaCode = areaCode;
        this.prefix = prefix;
        this.lineNumber = lineNumber;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PhoneNumber that = (PhoneNumber) o;

        if (areaCode != that.areaCode) return false;
        if (prefix != that.prefix) return false;
        return lineNumber == that.lineNumber;
    }

    @Override
    public int hashCode() {
        int result = areaCode;
        result = 31 * result + prefix;
        result = 31 * result + lineNumber;
        return result;
    }

    public static void main(String[] args){
        Map<PhoneNumber,String> phoneNumberStringMap = new HashMap<PhoneNumber,String>();  1)初始化
        phoneNumberStringMap.put(new PhoneNumber(123, 456, 7890), "honghailiang");         2)put存储
        System.out.println(phoneNumberStringMap.get(new PhoneNumber(123, 456, 7890)));     3)get获取

    }
}
1)初始化
/**
     * Constructs an empty <tt>HashMap</tt> with the default initial capacity
     * (16) and the default load factor (0.75).
     */
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }

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

2)put存储
/**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

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

/**
     * Implements Map.put and related methods
     *
     * @param hash hash for key
     * @param key the key
     * @param value the value to put
     * @param onlyIfAbsent if true, don't change existing value
     * @param evict if false, the table is in creation mode.
     * @return previous value, or null if none
     */
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)      //tab为空则创建
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)               //根据下标获取,如果没有(没发生碰撞(hash值相同))则直接创建
            tab[i] = newNode(hash, key, value, null);
        else {                                                   //如果发生了碰撞进行如下处理
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode)                      //为红黑数的情况
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {                                               //为链表的情况,普通Node
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null); //链表保存
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);                //如果链表长度超过了8则转为红黑树
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key                     // 写入,并返回oldValue
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)          // 超过load factor*current capacity,resize
            resize();
        afterNodeInsertion(evict);
        return null;
    }

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

/**
     * Computes key.hashCode() and spreads (XORs) higher bits of hash
     * to lower.  Because the table uses power-of-two masking, sets of
     * hashes that vary only in bits above the current mask will
     * always collide. (Among known examples are sets of Float keys
     * holding consecutive whole numbers in small tables.)  So we
     * apply a transform that spreads the impact of higher bits
     * downward. There is a tradeoff between speed, utility, and
     * quality of bit-spreading. Because many common sets of hashes
     * are already reasonably distributed (so don't benefit from
     * spreading), and because we use trees to handle large sets of
     * collisions in bins, we just XOR some shifted bits in the
     * cheapest possible way to reduce systematic lossage, as well as
     * to incorporate impact of the highest bits that would otherwise
     * never be used in index calculations because of table bounds.
     */
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

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

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

/**
     * Basic hash bin node, used for most entries.  (See below for
     * TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
     */
    static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;

        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }

        public final K getKey()        { return key; }
        public final V getValue()      { return value; }
        public final String toString() { return key + "=" + value; }

        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                    Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
    }

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

/**
     * Entry for Tree bins. Extends LinkedHashMap.Entry (which in turn
     * extends Node) so can be used as extension of either regular or
     * linked node.
     */
    static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
        TreeNode<K,V> parent;  // red-black tree links
        TreeNode<K,V> left;
        TreeNode<K,V> right;
        TreeNode<K,V> prev;    // needed to unlink next upon deletion
        boolean red;
        TreeNode(int hash, K key, V val, Node<K,V> next) {
            super(hash, key, val, next);
        }
......
}

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
/**
     * Returns the value to which the specified key is mapped,
     * or {@code null} if this map contains no mapping for the key.
     *
     * <p>More formally, if this map contains a mapping from a key
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
     * key.equals(k))}, then this method returns {@code v}; otherwise
     * it returns {@code null}.  (There can be at most one such mapping.)
     *
     * <p>A return value of {@code null} does not <i>necessarily</i>
     * indicate that the map contains no mapping for the key; it's also
     * possible that the map explicitly maps the key to {@code null}.
     * The {@link #containsKey containsKey} operation may be used to
     * distinguish these two cases.
     *
     * @see #put(Object, Object)
     */
    public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }

get方法又用到了hash(),是根据key的hash和key获取Node,返回的值就是Node的value属性。下面主要看下getNode方法即可
/**
     * Implements Map.get and related methods
     *
     * @param hash hash for key
     * @param key the key
     * @return the node, or null if none
     */
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {                    //map中存在的情况,不存在则直接返回null
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))     //第一个直接命中
                return first;
            if ((e = first.next) != null) {                             //如果第一个没命中,获取下一个节点
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);   //如果下一个节点是TreeNode,则用getTreeNode当时获取
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))    //循环节点链表,直到命中
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }

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注释掉都不能得到正确的结果:
public class PhoneNumber {
    private int areaCode;
    private int prefix;
    private int lineNumber;

    public PhoneNumber(int areaCode, int prefix, int lineNumber) {
        this.areaCode = areaCode;
        this.prefix = prefix;
        this.lineNumber = lineNumber;
    }

//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//
//        PhoneNumber that = (PhoneNumber) o;
//
//        if (areaCode != that.areaCode) return false;
//        if (prefix != that.prefix) return false;
//        return lineNumber == that.lineNumber;
//    }

    @Override
    public int hashCode() {
        int result = areaCode;
        result = 31 * result + prefix;
        result = 31 * result + lineNumber;
        return result;
    }

    public static void main(String[] args){
        Map<PhoneNumber,String> phoneNumberStringMap = new HashMap<PhoneNumber,String>();
        phoneNumberStringMap.put(new PhoneNumber(123, 456, 7890), "honghailiang");
        System.out.println(phoneNumberStringMap.get(new PhoneNumber(123, 456, 7890)));
    }
}

上述结果均为null;


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



【Java实战】源码解析为什么覆盖equals方法时总要覆盖hashCode方法的更多相关文章

  1. 【转】Java HashMap 源码解析(好文章)

    ­ .fluid-width-video-wrapper { width: 100%; position: relative; padding: 0; } .fluid-width-video-wra ...

  2. Java——LinkedHashMap源码解析

    以下针对JDK 1.8版本中的LinkedHashMap进行分析. 对于HashMap的源码解析,可阅读Java--HashMap源码解析 概述   哈希表和链表基于Map接口的实现,其具有可预测的迭 ...

  3. Java集合类源码解析:Vector

    [学习笔记]转载 Java集合类源码解析:Vector   引言 之前的文章我们学习了一个集合类 ArrayList,今天讲它的一个兄弟 Vector.为什么说是它兄弟呢?因为从容器的构造来说,Vec ...

  4. Java集合类源码解析:AbstractMap

    目录 引言 源码解析 抽象函数entrySet() 两个集合视图 操作方法 两个子类 参考: 引言 今天学习一个Java集合的一个抽象类 AbstractMap ,AbstractMap 是Map接口 ...

  5. Java集合类源码解析:LinkedHashMap

    前言 今天继续学习关于Map家族的另一个类 LinkedHashMap .先说明一下,LinkedHashMap 是继承于 HashMap 的,所以本文只针对 LinkedHashMap 的特性学习, ...

  6. Java集合类源码解析:HashMap (基于JDK1.8)

    目录 前言 HashMap的数据结构 深入源码 两个参数 成员变量 四个构造方法 插入数据的方法:put() 哈希函数:hash() 动态扩容:resize() 节点树化.红黑树的拆分 节点树化 红黑 ...

  7. Java - TreeMap源码解析 + 红黑树

    Java提高篇(二七)-----TreeMap TreeMap的实现是红黑树算法的实现,所以要了解TreeMap就必须对红黑树有一定的了解,其实这篇博文的名字叫做:根据红黑树的算法来分析TreeMap ...

  8. Java集合类源码解析:ArrayList

    目录 前言 源码解析 基本成员变量 添加元素 查询元素 修改元素 删除元素 为什么用 "transient" 修饰数组变量 总结 前言 今天学习一个Java集合类使用最多的类 Ar ...

  9. java集合 源码解析 学习手册

    学习路线: http://www.cnblogs.com/skywang12345/ 总结 1 总体框架 2 Collection架构 3 ArrayList详细介绍(源码解析)和使用示例 4 fai ...

随机推荐

  1. Jquery2 基础核心

    学习要点: 1.代码风格 2.加载模式 3.对象互换 4.多个库之间的冲突 本节简单的介绍一下jQuery 一些核心的问题. 一.代码风格 在jQuery程序中,不管是页面元素的选择.内置的功能函数, ...

  2. 戴尔服务器使用omreport(OMSA)查看监控硬件信息

    安装OMSA wget -q -O - http://linux.dell.com/repo/hardware/latest/bootstrap.cgi | bash yum install -y n ...

  3. journalctl 工具使用

    在Systemd出现之前,Linux系统及各应用的日志都是分别管理的,Systemd开始统一管理了所有Unit的启动日志,这样带来的好处就是可以只用一个 journalctl命令,查看所有内核和应用的 ...

  4. java开发中的诡异事件

    1.Excel中的诡异'\r' 2.springMVC中参数莫名的不匹配 今天在使用springmvc测试一个controller方法一直访问失败,400错误,百思不得其解 先是问同事,然后一起讨论了 ...

  5. 【Network Architecture】Densely Connected Convolutional Networks 论文解析

    目录 0. Paper link 1. Overview 2. DenseNet Architecture 2.1 Analogy to ResNet 2.2 Composite function 2 ...

  6. POJ2739解题报告

    2017-09-01 17:04:45 writer:pprp 一开始读错题了,总是想不到,其实不是很难,但是就是心理太着急了,反而浪费了很长时间 /* @param:poj2739 @writer: ...

  7. 【乱码】Request QueryString 编码,传值乱码的几种情况和解决办法(单页,多页)

    自己的项目还好,合作接口神马的传值乱码是很常见的. 遇到了几次这种恶心情况,积累了一点经验,有不对的地方欢迎指正. 乱码原因: 一般来说,常用的编码不是utf-8就是gb2312,一样的 HttpUt ...

  8. SecureCRT在mac下无法输入中断命令

    mac下输入Ctrl +C无法中断程序,这个问题困扰了我好久,大概有很长一段时间我都是使用kill 进程的方式来代替中断: ps aux | grep python kill -9 pid 今天终于发 ...

  9. css实现一色多变化

    .pesudo{ position: absolute; top:50%; left: 50%; transform:translate(-50%,-50%); width: 120px; paddi ...

  10. Nginx安装和使用

    Nginx简介 nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态.静态页面的分离,可以按照轮询.ip哈希.URL哈希.权重等多种方式对后 ...