HashMap学习

作者: enjoycc97 | 来源:发表于2018-07-28 15:59 被阅读0次

    调用如下:

    HashMap<String,String> map=new HashMap();
    map.put("1","1");
    String value=map.get("1");

    目的

    在于挖掘事物本身:
    其实之前也看过很多介绍的,还说JDK1,7和JDK1.8区别,如果本身你只会调用其实还挺痛苦的。我的理解是知道事情背后原理,这样你可以更好的理解,同时你本身也可以显得更加
    专业。

    构造方法

    没有什么大不了。就这样咯。

    public HashMap() {
            this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
        }
    

    put学习:

    putVal(hash(key), key, value, false, true)

    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)
                n = (tab = resize()).length;
            if ((p = tab[i = (n - 1) & hash]) == null)
    //很经典,取模运算,找到指定数组index,有可能挂着的链表为空
                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 {
                    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);
                            break;
                        }
    //链表上找到了 这里break是因为上面e=p,next 已经赋值,e代表existNode
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            break;
                        p = e;
                    }
                }
                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)
                resize();
            afterNodeInsertion(evict);
            return null;
        }
    

    这里需要学习一下hash算法,
    1.8的hash思路

     int h;
     return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    

    hash算法本身先这样,不然绕进去很头痛的。暂时先可以不管他。
    继续put流程

    image.png

    这张图解释的很清楚了,其实看这张图也很好解释HashMap内部那些key啊,value啊
    到底是怎么存储的。
    这张图分析就是一个数组,每个数组又去挂着一串链表。
    put大致原理分析如下:
    先找到hash取模运算,找到到底是数组哪一个,然后去数组上面的链表找,
    其实你可以自己想一下流程,不外乎就是先去找人玩不也是先去楼栋号然后门牌号。
    差不多就是这个意思,你可以看上面贴图的代码。要么已经存在,要么找不到,最终也就是在指定的地方写入新Node或者修改已有的Node。

    再看get

    其实也是差不多逻辑。
    找一下hash取模运算。在数组上索引位置,先看key啊是不是一样,
    一样就找到直接返回。否则去链表一个个next找即可,如果链表挂着
    是TreeNode直接交给TreeNode找

      /**
         * 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) {
    //取模运算,找到对应的key
                if (first.hash == hash && // always check first node
                    ((k = first.key) == key || (key != null && key.equals(k))))
                    return first;
                if ((e = first.next) != null) {
    //TreeNode交给TreeNode去做
                    if (first instanceof TreeNode)
                        return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                    do {
    //链表依次去一个个找
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            return e;
                    } while ((e = e.next) != null);
                }
            }
            return null;
        }
    

    遗留:TreeNode是什么,不是图中挂着链表吗?
    获取指定对象是根据Hashcode来找到的,那么equals重写了而hashCode没有重写会不会找不到啊。
    还有扩容原理,扩容消耗。0.75因子

    相关文章

      网友评论

        本文标题:HashMap学习

        本文链接:https://www.haomeiwen.com/subject/yelomftx.html