前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HashMap原理阅读

HashMap原理阅读

作者头像
Ryan-Miao
发布2018-03-14 10:52:57
5930
发布2018-03-14 10:52:57
举报
文章被收录于专栏:Ryan MiaoRyan Miao

前言

还是需要从头阅读下HashMap的源码。目标在于更好的理解HashMap的用法,学习更精炼的编码规范,以及应对面试。

它根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而具有很快的访问速度,但遍历顺序却是不确定的。 HashMap最多只允许一条记录的键为null,允许多条记录的值为null。HashMap非线程安全,即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致。如果需要满足线程安全,可以用 Collections的synchronizedMap方法使HashMap具有线程安全的能力,或者使用ConcurrentHashMap。

面试官: 说说HashMap的原理

答: HashMap是通过哈希表的数组链表实现的。内部维护一个Node数组,

当put时,计算key hash后的值当做索引。如果数组中该位置为null,则放入value。然后判断是否需要扩容,返回null。

如果数组上已经有元素,判断hash和key是否相等,相等就表示找到node节点了,不相等则判断该元素是TreeNode还是普通Node。

如果是TreeNode,则按照TreeNode的put方法插入。

如果不是TreeNode, 遍历链表,对比hash和key,若都不相等,则插入队尾,如果链表长度大于等于8,将链表转换为TreeNode.

找到node之后,node不为null则赋值value。最后返回原来的value。

完毕。

面试官: 如何扩容

答:(直接说1.8的内容,想要装逼体验深度就对比1.7. 比如1.7扩容会导致链表重排倒置,1.8不会,1.8不用再次计算hash等。当然,这样回答要准备好继续入坑,为什么,如何做到)

要说扩容,首先要知道原来的容量以及什么时候扩容。HashMap初始化的时候可以指定initialCapacityloadfactorcapacity是2的指数倍,表示数组的长度。

loadfactor表示达到容量的百分比后扩容。threshold=capacity*loadfactor就是HashMap对象中可以容纳的最大K-V键值对数量。

所以,当size(当前K-V键值对数量)超过threshold,则进行扩容。当然,如果capacity已经大于2^30,则直接将threshold=Integer.MAX_VALUE, 就不扩容了,碰撞吧。

扩容的时候先计算容量,扩大为原来的2倍,对应threshold也扩大为原来的2倍。

然后将原来数组上的元素复制到新的数组。对于冲突碰撞的结点,是TreeNode则按TreeNode插入,不是TreeNode则将链表的一半平分到其他新增的索引位置。

关于几个数字。loadfactor=0.75; DEFAULT_INITIAL_CAPACITY = 1 << 4; MAXIMUM_CAPACITY = 1 << 30; TREEIFY_THRESHOLD = 8。也就是说,对于我们平时直接new 的HashMap对象,默认数组长度为16,最大容纳12个,超过12个则扩容;当发生碰撞的数量小于8个则维护链表,当数量大于8个则改造成TreeNode.

面试官: 说TreeNode是怎么put的

红黑树啊,红黑树我不会写。

面试官: 如何get

答: 既然知道HashMap的存储原理,那个get也就呼之欲出了。 首先,计算hash索引,如果头结点不为null,如果头结点hash以及key都相等,则取出。

如果头结点不相等,并且next不为nul,判断next是否是TreeNode, 如果是TreeNode则TreeNode get.

如果不是TreeNode, 遍历链表,找到hash和key相等的取出value。

在这里,非常感谢美团技术博客中的《Java 8系列之重新认识HashMap》, 深入,透彻,易懂。

面试官: HashMap是线程安全的吗

答:不是,高并发中不仅会不安全,还有可能造成死循环(扩容的时候)。想要在并发中使用,请使用ConcurrentHashMap.

初始化,构造函数

代码语言:javascript
复制
HashMap<String, Object> map = new HashMap<>();

对应源码为:

代码语言:javascript
复制
/**
 * 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,默认容量(capacity)为16,默认负载因子(load factor)是0.75.

Put

代码语言:javascript
复制
/**
 * 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);
}
  1. put的key需要计算hashcode
  2. put的value可以是任何对象
  3. 如果key存在则替换并返回前一个对象
代码语言:javascript
复制
/**
 * 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);
}

重新计算hash,但仍旧根据key的hashcode的方法。

代码语言:javascript
复制
/**
 * 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) {
    //....
}
  1. 核心put方法,第一个参数是key的hash之后再hash. 这里,我开始有个疑问,就是发现所有调用putVal的地方的第一个参数的hash的计算方式都是一样的,觉得应该去掉第一个参数,直接在这个方法里hash(Key)就好了。事实上,确实可以这么做,但带来的问题是所有调用这个方法的地方都要用同样的hash方法。
  2. 第二个参数是key
  3. 第三个参数是value
  4. 第4个参数是区分putIfAbsent(k,v)的标志,true表示如果不存在则存储,已经存在则不存储;默认false,即覆盖。
  5. 第5个参数evict是逐出的意思,只在LinkedHashMap中有用,本处空调用。

存储原理概述

首先,需要准备背景知识,关于数字二进制表示,左移右移等。参阅 http://www.cnblogs.com/woshimrf/p/operation-bit.html

PutVal()

代码语言:javascript
复制
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
    //内部数组
    Node<K,V>[] tab; 
    //指针
    Node<K,V> p; 
    //数组长度,索引
    int n, i;
    
    //初始化数组,对于新建的对象,没有put的时候是没有创建数组的
    if ((tab = table) == null || (n = tab.length) == 0)
        n = (tab = resize()).length;
        
    //计算索引,若当前结点为null,则直接直插入,完毕到返回。
    if ((p = tab[i = (n - 1) & hash]) == null)
        tab[i] = newNode(hash, key, value, null);
    else {
        Node<K,V> e; K k;  //e找到的结点,
        
        //如果当前结点hash相同,key相同,则找到结点
        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 {
            //遍历链表,p是指针,e为找到的结点
            for (int binCount = 0; ; ++binCount) {
                //遍历到尾结点后直接插入尾部新结点,此时e==null, 不参与后面的value覆盖逻辑。
                if ((e = p.next) == null) {
                    p.next = newNode(hash, key, value, null);
                    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st, 当链表长度大于8后转换为红黑树
                        treeifyBin(tab, hash);
                    break;
                }
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                p = e;
            }
        }
        
        //找到结点后决定是否覆盖value
        if (e != null) { // existing mapping for key
            V oldValue = e.value;
            if (!onlyIfAbsent || oldValue == null)
                e.value = value;
            afterNodeAccess(e);
            return oldValue;
        }
    }
    
    ++modCount;//统计数组上的结点数量
    if (++size > threshold)   //当前K-V数量超过threshold后扩容
        resize();
    afterNodeInsertion(evict);
    return null;  //因为执行到此处的代码都是新插入的结点,所以返回空。
}

resize()

代码语言:javascript
复制
final Node<K,V>[] resize() {
    //保存旧的数组
    Node<K,V>[] oldTab = table;
    int oldCap = (oldTab == null) ? 0 : oldTab.length;
    int oldThr = threshold;
    int newCap, newThr = 0;
    
    if (oldCap > 0) {
        // 超过最大值就不再扩充了,就只好随你碰撞去吧
        if (oldCap >= MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return oldTab;
        }
        // 没超过最大值,就扩充为原来的2倍
        else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                 oldCap >= DEFAULT_INITIAL_CAPACITY)
            newThr = oldThr << 1; // double threshold
    }
    else if (oldThr > 0) // initial capacity was placed in threshold
        newCap = oldThr;
    else {               // zero initial threshold signifies using defaults
        newCap = DEFAULT_INITIAL_CAPACITY;
        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
    }
    if (newThr == 0) {
        //出现了,threshold的计算公式
        float ft = (float)newCap * loadFactor;
        newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                  (int)ft : Integer.MAX_VALUE);
    }
    threshold = newThr;
    //用新的capacity来创建数组
    @SuppressWarnings({"rawtypes","unchecked"})
        Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
    table = newTab;
    if (oldTab != null) {
        //开始迁移复制
        for (int j = 0; j < oldCap; ++j) {
            Node<K,V> e;
            if ((e = oldTab[j]) != null) {
                oldTab[j] = null;
                if (e.next == null) //数组结点直接复制
                    newTab[e.hash & (newCap - 1)] = e;
                else if (e instanceof TreeNode)   //红黑树结点
                    ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                else { // preserve order 对于碰撞的链表,优化,取出一半的结点到新的数组结点
                    Node<K,V> loHead = null, loTail = null;
                    Node<K,V> hiHead = null, hiTail = null;
                    Node<K,V> next;
                    do {
                        next = e.next;
                        // 原索引
                        if ((e.hash & oldCap) == 0) {
                            if (loTail == null)
                                loHead = e;
                            else
                                loTail.next = e;
                            loTail = e;
                        }
                         // 原索引+oldCap
                        else {
                            if (hiTail == null)
                                hiHead = e;
                            else
                                hiTail.next = e;
                            hiTail = e;
                        }
                    } while ((e = next) != null);
                    // 原索引放到bucket里
                    if (loTail != null) {
                        loTail.next = null;
                        newTab[j] = loHead;
                    }
                    // 原索引+oldCap放到bucket里
                    if (hiTail != null) {
                        hiTail.next = null;
                        newTab[j + oldCap] = hiHead;
                    }
                }
            }
        }
    }
    return newTab;
}

参考

https://tech.meituan.com/java-hashmap.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
    • 面试官: 说说HashMap的原理
      • 面试官: 如何扩容
        • 面试官: 说TreeNode是怎么put的
          • 面试官: 如何get
            • 面试官: HashMap是线程安全的吗
            • 初始化,构造函数
            • Put
            • 存储原理概述
              • PutVal()
                • resize()
                • 参考
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档