JDK11版HashMap源码全部解析(详细)-一文覆盖各方面

1. 概述

本文很长,详细描述了HashMap源码级别的实现原理,并讨论了包括扩容,hash计算,新建HashMap的开销等问题,同时还提供了一些外部资料。由于内容太多,建议阅读时结合目录快速跳转查看。

Java源码阅读最好采用IDEA,Ctrl + N 输入HashMap即可看到HashMap的源码了,HashMap总共有2444行源码
本文查看的是JDK-11.0.1的源码

文章目录

  • 1. 概述
    • 1.1 内部类
    • 1.2 基本实现
    • 1.3 扩容原理
    • 1.4 hash计算
      • 为什么这样计算hash值
    • 1.5 元素实际位置计算
    • 1.6 插入null原理
    • 1.7 new HashMap()开销
  • 2. HashMap的变量
    • 2.1 DEFAULT_INITIAL_CAPACITY
    • 2.2 MAXIMUM_CAPACITY
    • 2.3 DEFAULT_LOAD_FACTOR
    • 2.4 TREEIFY_THRESHOLD
    • 2.5 UNTREEIFY_THRESHOLD
    • 2.6 MIN_TREEIFY_CAPACITY
    • 2.7 table
    • 2.8 entrySet
    • 2.9 size
    • 2.10 modCount
    • 2.11 threshold
    • 2.12 loadFactor
    • 2.13 serialVersionUID
  • 3. HashMap的方法
    • 3.1 构造函数
    • 3.2 hash(Object key)
    • 3.3 comparableClassFor(Object x)
    • 3.4 compareComparables(Class kc, Object k, Object x)
    • 3.5 tableSizeFor(int cap)
    • 3.6 putMapEntries(Map m, boolean evict)
    • 3.7 size()
    • 3.8 isEmpty()
    • 3.9 get(Object key)
    • 3.10 getNode(int hash, Object key)
    • 3.11 containsKey(Object key)
    • 3.12 put(K key, V value)
    • 3.13 putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict)
    • 3.14 resize() 扩容
    • 3.15 treeifyBin(Node < K,V > [] tab, int hash)
    • 3.16 putAll(Map m)
    • 3.17 remove(Object key)
    • 3.18 removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable)
    • 3.19 clear()
    • 3.20 containsValue(Object value)
    • 3.21 keySet()
    • 3.22 values()
    • 3.23 entrySet()
    • 3.24 getOrDefault(Object key, V defaultValue)
    • 3.25 putIfAbsent(K key, V value)
    • 3.26 remove(Object key, Object value)
    • 3.27 replace(K key, V oldValue, V newValue)
    • 3.28 replace(K key, V value)
    • 3.29 computeIfAbsent(K key, Function mappingFunction)
    • 3.30 computeIfPresent(K key, BiFunction remappingFunction)
    • 3.31 compute(K key, BiFunction remappingFunction)
    • 3.32 merge(K key, V value, BiFunction remappingFunction)
    • 3.33 forEach(BiConsumer action)
    • 3.34 replaceAll(BiFunction function)
    • 3.35 clone()
    • 3.36 loadFactor()
    • 3.37 capacity()
    • 3.38 writeObject(java.io.ObjectOutputStream s)
    • 3.39 readObject(java.io.ObjectInputStream s)
    • 3.41 replacementNode(Node < K,V > p, Node < K,V > next)
    • 3.42 newTreeNode(int hash, K key, V value, Node < K,V > next)
    • 3.43 replacementTreeNode(Node < K,V > p, Node < K,V > next)
    • 3.44 reinitialize()
    • 3.45 afterNodeAccess(Node < K,V > p)
    • 3.46 afterNodeInsertion(boolean evict)
    • 3.47 afterNodeRemoval(Node < K,V > p)
    • 3.48 internalWriteEntries(java.io.ObjectOutputStream s)
  • 4. HashMap的内部类
    • 4.1 Node
    • 4.2 KeySet
    • 4.3 EntrySet
    • 4.4 TreeNode
  • 后记
  • 参考资料

咱们按照源码顺序来分析HashMap,除了HashMap本身的变量和方法,HashMap中还定义了定义如下内部类:

1.1 内部类

  1. Node
  2. KeySet
  3. Values
  4. EntrySet
  5. HashIterator
  6. KeyIterator
  7. ValueIterator
  8. EntryIterator
  9. HashMapSpliterator
  10. KeySpliterator
  11. ValueSpliterator
  12. EntrySpliterator
  13. TreeNode:这个类代表红黑树节点,HashMap中对红黑树的操作的方法都在此类中

1.2 基本实现

HashMap底层使用哈希表(数组 + 单链表),当链表过长会将链表转成 红黑树以实现 O(logn) 时间复杂度内查找。

HashMap的定义class HashMap extends AbstractMap implements Map, Cloneable, Serializable

1.3 扩容原理

HashMap采用的扩容策略是,每次加倍,这样,原来位置的Entry在新扩展的数组中要么依然在原来的位置,要么在<原来位置+原来的容量>的位置。

1.4 hash计算

hash()函数计算hash值方法为(key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16),计算出的hash值会被缓存在Node.hash中。

为什么这样计算hash值

hash值计算相当于就是将高16位与低16位进行异或,结果是高16不变,低16位变成其异或的新结果。

为什么让低16位与高16为异或合成一个新的结果呢?是因为HashMap的容量通常比较小,在进行长度取模运算时采用的是只取二进制最右端几位,这样高位的二进制信息就没有用到,所带来的结果就是Hash结果分布不太均匀。而高16位与低16位异或后就可以让低位附带高位的信息,加大低位的随机性。具体请参考JDK 源码中 HashMap 的 hash 方法原理是什么? - 胖君的回答 - 知乎

不明白异或结果的朋友来看下这段验证代码,复制此代码运行即可明白高16位与低16位的异或的结果:

import java.util.Random;

class Scratch {
    public static void main(String[] args) {
        generateTestCase(41132564);

        Random random = new Random();
        for (int j = 0; j < 10; j++) {
            generateTestCase(random.nextInt(Integer.MAX_VALUE));
        }
    }

    /**
     * 显示根据key的hashCode算出最终元素的hash值
     *
     * @param hashCode 代表key的hashCode
     */
    public static void generateTestCase(int hashCode) {
        System.out.println("hashCode = " + hashCode + " 时");
        show(hashCode);

        int k = hashCode >>> 16;
        show(k);

        int x = hashCode ^ k;
        show(x);

        System.out.println();
    }

    /**
     * 显示一个数字的二进制,按照高16位在左,低16位在右的方式显示
     */
    public static void show(int n) {
        String s = Integer.toBinaryString(n);
        s = fillZero(s);

        System.out.print(s.substring(0, 16));
        System.out.print("  |  ");
        System.out.println(s.substring(16));
    }

    /**
     * 填充0到字符串前面使得总长32
     */
    public static String fillZero(String src) {
        StringBuilder sb = new StringBuilder(32);
        for (int i = 0; i < 32 - src.length(); i++) {
            sb.append('0');
        }
        return sb.append(src).toString();
    }
}

这就是为什么HashMap可以放入键值null,因为计算hash中为null的hash值为0,然后putVal插入

1.5 元素实际位置计算

根据hash()获取元素所在链表的位置的方法为:tab[(n - 1) & hash],由于n为容量是2的幂,n-1的二进制形式是111111这类二进制左边全1的形式,所以这个方法本质是截取hash二进制相应长度的0和1,如下例。

hash:   10111101
n - 1:  00111111
result: 00111101
// hash的最左端的1没有了,相当于只取二进制最右端几位

1.6 插入null原理

在hash计算中(上文),null的hash值为0,然后按照正常的putVal()插入

1.7 new HashMap()开销

从源码中(下文构造函数)我们可以看到:

new HashMap()开销非常少,仅仅确认装载因子。真正的创建table的操作尽可能的往后延迟,这使得HashMap有不少操作都需要检查table是否初始化。这种设计我猜想应该是为了让人们可以不用担心创建HashMap的开销,大量创建HashMap,比如ArrayList a = new ArrayList<>(1000)

2. HashMap的变量

2.1 DEFAULT_INITIAL_CAPACITY

HashMap的默认容量是16,被DEFAULT_INITIAL_CAPACITY定义。

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

2.2 MAXIMUM_CAPACITY

其最大容量为 1073741824(2的30次方)

static final int MAXIMUM_CAPACITY = 1 << 30;

2.3 DEFAULT_LOAD_FACTOR

默认装载因子0.75

static final float DEFAULT_LOAD_FACTOR = 0.75f;

2.4 TREEIFY_THRESHOLD

将链表转换成红黑树的阈值为8,即当链表长度>=8时,链表转换成红黑树

static final int TREEIFY_THRESHOLD = 8;

2.5 UNTREEIFY_THRESHOLD

将红黑树转换成链表的阈值为6(<6时转换),注意,这个是在resize()的过程中调用TreeNode.split()实现

static final int UNTREEIFY_THRESHOLD = 6;

2.6 MIN_TREEIFY_CAPACITY

要树化并不仅仅是超过TREEIFY_THRESHOLD ,同时容量要超过MIN_TREEIFY_CAPACITY,如果只是超过TREEIFY_THRESHOLD,则会进行扩容(调用resize(),因为扩容可以让链表变短),直到扩容>=MIN_TREEIFY_CAPACITY

static final int MIN_TREEIFY_CAPACITY = 64;

2.7 table

哈希表的数组主体定义,使用时初始化,在构造函数中并不会初始化,所以在各种操作中总是要检查其是否为null

transient Node<K,V>[] table;

2.8 entrySet

作为一个entrySet缓存,使用entrySet方法首先检查其是否为null,不为null则使用这个缓存,否则生成一个并缓存至此。

transient Set<Map.Entry<K,V>> entrySet;

2.9 size

HashMap中Entry的数量

transient int size;

2.10 modCount

记录修改内部结构化修改次数,用于实现fail-fast,ConcurrentModificationException就是通过检测这个抛出

transient int modCount;

2.11 threshold

其值=capacity * load factor,当size超过threshhold便进行一次扩容

int threshold;

2.12 loadFactor

装载因子

final float loadFactor;

2.13 serialVersionUID

用于序列化

private static final long serialVersionUID = 362498820763181265L;

3. HashMap的方法

3.1 构造函数

  1. 该构造函数并不初始化transient Node[] table;,进行容量和装载因子的(范围)合法性验证,然而并没有对容量进行存储,只是用来确定扩容阈值threshold

    public HashMap(int initialCapacity, float loadFactor) {
            if (initialCapacity < 0)
                throw new IllegalArgumentException("Illegal initial capacity: " +
                                                   initialCapacity);
            if (initialCapacity > MAXIMUM_CAPACITY)
                initialCapacity = MAXIMUM_CAPACITY;
            if (loadFactor <= 0 || Float.isNaN(loadFactor))
                throw new IllegalArgumentException("Illegal load factor: " +
                                                   loadFactor);
            this.loadFactor = loadFactor;
            this.threshold = tableSizeFor(initialCapacity);
        }
    
  2. 显然

    public HashMap(int initialCapacity) {
            this(initialCapacity, DEFAULT_LOAD_FACTOR);
        }
    
  3. 无参构造函数仅仅确认装载因子

    public HashMap() {
            this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
        }
    
  4. 通过Map构造HashMap时,使用默认装载因子,并调用putMapEntries将Map装入HashMap

    public HashMap(Map<? extends K, ? extends V> m) {
            this.loadFactor = DEFAULT_LOAD_FACTOR;
            putMapEntries(m, false);
        }
    

3.2 hash(Object key)

Hash函数负责产生HashCode,计算法则为若key为null则返回0,否则:对key的hashCode的高16位和低16位进行异或

static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);// >>>表示无符号右移
}

3.3 comparableClassFor(Object x)

对于一个Object,若其定义时是class X implement Comparable,则返回X,否则返回null,注意一定Comparable中的X一定得是X不能为其子类或父类,用于红黑树中的比较

  • 源码如下
static Class<?> comparableClassFor(Object x) {
        if (x instanceof Comparable) {
            Class<?> c; Type[] ts, as; ParameterizedType p;
            if ((c = x.getClass()) == String.class) // bypass checks
                return c;
            if ((ts = c.getGenericInterfaces()) != null) {
                for (Type t : ts) {
                    if ((t instanceof ParameterizedType) &&
                        ((p = (ParameterizedType) t).getRawType() ==
                         Comparable.class) &&
                        (as = p.getActualTypeArguments()) != null &&
                        as.length == 1 && as[0] == c) // type arg is c
                        return c;
                }
            }
        }
        return null;
    }
  • 验证代码如下,将如下代码拷贝即可明白其原理。
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
class Scratch {
    public static void main(String[] args) {
        System.out.println(comparableClassFor(new C()));// class Scratch$C
        System.out.println(comparableClassFor(new CS()));// null
        System.out.println(comparableClassFor(new CSI()));// null
        System.out.println(comparableClassFor(new CSIC()));// class Scratch$CSIC
    }
    static Class<?> comparableClassFor(Object x) {
        if (x instanceof Comparable) {
            Class<?> c; Type[] ts, as; ParameterizedType p;
            if ((c = x.getClass()) == String.class) // bypass checks
                return c;
            if ((ts = c.getGenericInterfaces()) != null) {
                for (Type t : ts) {
                    if ((t instanceof ParameterizedType) &&
                            ((p = (ParameterizedType) t).getRawType() ==
                                    Comparable.class) &&
                            (as = p.getActualTypeArguments()) != null &&
                            as.length == 1 && as[0] == c) // type arg is c
                        return c;
                }
            }
        }
        return null;
    }
    static class C implements Comparable<C> {
        @Override
        public int compareTo(C o) {
            return 0;
        }
    }
    static class CS extends C {}
    static class CSI implements Comparable<C> {
        @Override
        public int compareTo(C o) {
            return 0;
        }
    }
    static class CSIC implements Comparable<CSIC> {
        @Override
        public int compareTo(CSIC o) {
            return 0;
        }
    }
}

3.4 compareComparables(Class kc, Object k, Object x)

如果x=null,返回0;如果x的类型为kc,则返回k.compare(x);否则返回0

static int compareComparables(Class<?> kc, Object k, Object x) {
        return (x == null || x.getClass() != kc ? 0 :
                ((Comparable)k).compareTo(x));
    }

3.5 tableSizeFor(int cap)

对于给定cap,计算>=cap的2的幂。用于计算table数组大小

static final int tableSizeFor(int cap) {
        int n = -1 >>> Integer.numberOfLeadingZeros(cap - 1);
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

3.6 putMapEntries(Map m, boolean evict)

先确定放入map时容量是否应该调整,调整好后,通过putVal一个个放入

final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
        int s = m.size();
        if (s > 0) {//放入的map的size要大于0才插入
            if (table == null) { // pre-size,如果本map的table未初始化(同时没有任何元素),就根据放入map大小以及loadfactor计算出threshold,依然不初始化table
                float ft = ((float)s / loadFactor) + 1.0F;
                int t = ((ft < (float)MAXIMUM_CAPACITY) ?
                         (int)ft : MAXIMUM_CAPACITY);
                if (t > threshold)
                    threshold = tableSizeFor(t);
            }
            else if (s > threshold)//放入的map超过threshold就扩容
                resize();
            //到这里容量问题解决了,就一个一个putVal插入
            for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
                K key = e.getKey();
                V value = e.getValue();
                putVal(hash(key), key, value, false, evict);
            }
        }
    }

3.7 size()

直接返回size变量的值

public int size() {
        return size;
}

3.8 isEmpty()

判断size是否等于0

public boolean isEmpty() {
        return size == 0;
}

3.9 get(Object key)

通过计算hash并调用getNode找到节点Node,然后返回Node.value,找不到Node则返回null

public V get(Object key) {
    Node<K,V> e;
    return (e = getNode(hash(key), key)) == null ? null : e.value;
}

3.10 getNode(int hash, Object key)

如果table未初始化以及长度=0或者根据hash找到链表的第一个元素为nul便返回null,否则判断第一个节点是否为要找的节点,否则以后的节点根据红黑树类型或链表类型采用各自的查找策略

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) {//在table初始化了,且table长度大于0并且根据hash找到链表第一个节点不为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);
            do {//链表类型就是顺序遍历查找
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    return e;
            } while ((e = e.next) != null);
        }
    }
    return null;
}

3.11 containsKey(Object key)

根据getNode是否找到节点来判断是否存在

public boolean containsKey(Object key) {
    return getNode(hash(key), key) != null;
}

3.12 put(K key, V value)

调用putVal放入节点

public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}

3.13 putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict)

参数解释:

  • onlyIfAbsent:为true则不改变已有的值
  • evict:用于LinkedList使用,对于hashmap没用,如果为false,table进入创建模式()

先进行table初始化和0长检查判断是否需要扩容,之后如果key所在链表表头未初始化便初始化并插入,否则根据是否树化根据相应策略查找节点,如果onlyIfAbsent为false则插入。插入后size++并根据threshold判断是否扩容resize()

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)//table未初始化或0长便调用resize()初始化
        n = (tab = resize()).length;
    if ((p = tab[i = (n - 1) & hash]) == null)//如果相应位置的链表还未创建表头,便创建表头
        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;
                }
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                p = e;
            }
        }
        if (e != null) { // existing mapping for key,e便是插入的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;
}

3.14 resize() 扩容

阅读这部分源码应注意:

  • 所有的 Cap (即newCap,oldCap) 都是2的幂,即二进制为100000这类形式,Cap-1的二进制都为111111这类形式。

原理:

  • 先根据限制条件确定newThr和newCap,然后创建新的table,再把旧table的数据(即链表引用)复制过来。同时复制时根据hash值,在同一个链表中的元素根据oldCap(二进制是10000的形式)& e.hash(判断相应位为0还是1)来划分成两类,一类放在原位置,一类放在原位置+oldCap位置。
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;
        }
        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) {
        float ft = (float)newCap * loadFactor;
        newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                  (int)ft : Integer.MAX_VALUE);
    }
    //以上代码都是根据限制条件确定newThr和newCap
    threshold = newThr;
    @SuppressWarnings({"rawtypes","unchecked"})
    //创建新table
    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) {//根据oldCap确定元素hash指定位上是0还是1来划分放在原位置还是原位置+oldCap位置,这里是放在原位置
                            if (loTail == null)
                                loHead = e;
                            else
                                loTail.next = e;
                            loTail = e;
                        }
                        else {
                            if (hiTail == null)
                                hiHead = e;
                            else
                                hiTail.next = e;
                            hiTail = e;
                        }
                    } while ((e = next) != null);
                    if (loTail != null) {
                        loTail.next = null;
                        newTab[j] = loHead;//放在原位置
                    }
                    if (hiTail != null) {
                        hiTail.next = null;
                        newTab[j + oldCap] = hiHead;//原位置+oldCap
                    }
                }
            }
        }
    }
    return newTab;
}

3.15 treeifyBin(Node < K,V > [] tab, int hash)

达到树化阈值还需要超过MIN_TREEIFY_CAPACITY才会树化,否则先进行扩容操作,达到后,先将链表Node逐个替换成TreeNode,在调用TreeNode.treeify建立红黑树

final void treeifyBin(Node<K,V>[] tab, int hash) {
		int n, index; Node<K,V> e;
		if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)//达到树化阈值还需要超过MIN_TREEIFY_CAPACITY才会树化,否则先进行扩容操作
			resize();
		else if ((e = tab[index = (n - 1) & hash]) != null) {
			TreeNode<K,V> hd = null, tl = null;
			do {//先将链表Node逐个替换成TreeNode
				TreeNode<K,V> p = replacementTreeNode(e, null);
				if (tl == null)
					hd = p;
				else {
					p.prev = tl;
					tl.next = p;
				}
				tl = p;
			} while ((e = e.next) != null);
			if ((tab[index] = hd) != null)
				hd.treeify(tab);//这里进行树化
		}
	}

3.16 putAll(Map m)

putAll就是调用putMapEntries

public void putAll(Map<? extends K, ? extends V> m) {
	putMapEntries(m, true);
}

3.17 remove(Object key)

remove调用removeNode移除节点后,返回节点value

public V remove(Object key) {
		Node<K,V> e;
		return (e = removeNode(hash(key), key, null, false, true)) == null ?
			null : e.value;
}

3.18 removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable)

根据hash查找key的位置并删除,如果matchValue为true,则只有值也相等时才删除,如果movable为false,红黑树的删除不移动node,然后size减小,如果没找到key,返回null

final Node<K,V> removeNode(int hash, Object key, Object value,
							boolean matchValue, boolean movable) {
		Node<K,V>[] tab; Node<K,V> p; int n, index;
		if ((tab = table) != null && (n = tab.length) > 0 &&
			(p = tab[index = (n - 1) & hash]) != null) {
			Node<K,V> node = null, e; K k; V v;//查找key的位置
			if (p.hash == hash &&
				((k = p.key) == key || (key != null && key.equals(k))))
				node = p;
			else if ((e = p.next) != null) {
				if (p instanceof TreeNode)
					node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
				else {
					do {
						if (e.hash == hash &&
							((k = e.key) == key ||
							(key != null && key.equals(k)))) {
							node = e;
							break;
						}
						p = e;
					} while ((e = e.next) != null);
				}
			}
			if (node != null && (!matchValue || (v = node.value) == value ||
								(value != null && value.equals(v)))) {//matchValue控制值也要相等才删除
				if (node instanceof TreeNode)	//删除key并减小size
					((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
				else if (node == p)
					tab[index] = node.next;
				else
					p.next = node.next;
				++modCount;
				--size;
				afterNodeRemoval(node);
				return node;
			}
		}
		return null;
	}

3.19 clear()

size置0,table每个Node都置为null

public void clear() {
		Node<K,V>[] tab;
		modCount++;
		if ((tab = table) != null && size > 0) {
			size = 0;
			for (int i = 0; i < tab.length; ++i)
				tab[i] = null;
		}
	}

3.20 containsValue(Object value)

双重循环查找数组+链表中的每个value

public boolean containsValue(Object value) {
		Node<K,V>[] tab; V v;
		if ((tab = table) != null && size > 0) {
			for (Node<K,V> e : tab) {
				for (; e != null; e = e.next) {
					if ((v = e.value) == value ||
						(value != null && value.equals(v)))
						return true;
				}
			}
		}
		return false;
	}

3.21 keySet()

查看keySet中是否有已缓存的keySet,没有就创建并加入缓存。值得注意的是由于keySet采用视图技术(没有成员变量),所以创建开销近乎为0。

public Set<K> keySet() {
		Set<K> ks = keySet;
		if (ks == null) {
			ks = new KeySet();
			keySet = ks;
		}
		return ks;
	}

3.22 values()

查看是否有已缓存的,没有则创建Value()并缓存,由于没有成员变量,所以创建Values开销近乎为0

public Collection<V> values() {
	Collection<V> vs = values;
	if (vs == null) {
		vs = new Values();
		values = vs;
	}
	return vs;
}

3.23 entrySet()

查看是否有已缓存的,没有则创建EntrySet()并缓存,由于没有成员变量,所以创建开销近乎为0

public Set<Map.Entry<K,V>> entrySet() {
	Set<Map.Entry<K,V>> es;
	return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}

3.24 getOrDefault(Object key, V defaultValue)

用getNode查找,找不到则返回defaultValue

@Override
public V getOrDefault(Object key, V defaultValue) {
	Node<K,V> e;
	return (e = getNode(hash(key), key)) == null ? defaultValue : e.value;
}

3.25 putIfAbsent(K key, V value)

调用putVal,并设定putVal的参数onlyIfAbsent=true。只有不存在key的时候才插入

@Override
public V putIfAbsent(K key, V value) {
	return putVal(hash(key), key, value, true, true);
}

3.26 remove(Object key, Object value)

调用removeNode,返回是否删除成功

@Override
public boolean remove(Object key, Object value) {
	return removeNode(hash(key), key, value, true, true) != null;
}

3.27 replace(K key, V oldValue, V newValue)

通过getNode寻找到Node,判断oldValue是否与value相等,然后替换value,并有afterNodeAccess钩子用于LinkedHashMap

@Override
public boolean replace(K key, V oldValue, V newValue) {
    Node<K,V> e; V v;
    if ((e = getNode(hash(key), key)) != null &&
        ((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
        e.value = newValue;
        afterNodeAccess(e);
        return true;
    }
    return false;
}

3.28 replace(K key, V value)

通过getNode寻找到Node,然后替换value,并有afterNodeAccess钩子用于LinkedHashMap

@Override
public V replace(K key, V value) {
	Node<K,V> e;
	if ((e = getNode(hash(key), key)) != null) {
		V oldValue = e.value;
		e.value = value;
		afterNodeAccess(e);
		return oldValue;
	}
	return null;
}

3.29 computeIfAbsent(K key, Function mappingFunction)

如果查找不到key不就运行函数,并将函数返回值插入到key中。function不能更改modCount(不能修改HashMap),否则会ConcurrentModificationException

@Override
public V computeIfAbsent(K key,
						Function<? super K, ? extends V> mappingFunction) {
	if (mappingFunction == null)
		throw new NullPointerException();
	int hash = hash(key);
	Node<K,V>[] tab; Node<K,V> first; int n, i;
	int binCount = 0;
	TreeNode<K,V> t = null;
	Node<K,V> old = null;
	if (size > threshold || (tab = table) == null ||
		(n = tab.length) == 0)
		n = (tab = resize()).length;
	if ((first = tab[i = (n - 1) & hash]) != null) {
		if (first instanceof TreeNode)
			old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
		else {
			Node<K,V> e = first; K k;
			do {
				if (e.hash == hash &&
					((k = e.key) == key || (key != null && key.equals(k)))) {
					old = e;
					break;
				}
				++binCount;
			} while ((e = e.next) != null);
		}
		V oldValue;
		if (old != null && (oldValue = old.value) != null) {//找到后便返回并处理钩子
			afterNodeAccess(old);
			return oldValue;
		}
	}//没找到时
	int mc = modCount;
	V v = mappingFunction.apply(key);
	if (mc != modCount) { throw new ConcurrentModificationException(); }
	//将mappingFunction产生的返回值作为value插入到key中,如果超过树化阈值就树化,size++
	if (v == null) {
		return null;
	} else if (old != null) {
		old.value = v;
		afterNodeAccess(old);
		return v;
	}
	else if (t != null)
		t.putTreeVal(this, tab, hash, key, v);
	else {
		tab[i] = newNode(hash, key, v, first);
		if (binCount >= TREEIFY_THRESHOLD - 1)
			treeifyBin(tab, hash);
	}
	modCount = mc + 1;
	++size;
	afterNodeInsertion(true);
	return v;
}

3.30 computeIfPresent(K key, BiFunction remappingFunction)

如果查找到key就运行remappingFunction,并将返回值作为value插入。function不能更改modCount(不能修改HashMap),否则会ConcurrentModificationException。如果返回值为null,就通过removeNode删除key

    @Override
	public V computeIfPresent(K key,
							BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
		if (remappingFunction == null)
			throw new NullPointerException();
		Node<K,V> e; V oldValue;
		int hash = hash(key);
		if ((e = getNode(hash, key)) != null &&
			(oldValue = e.value) != null) {//
			int mc = modCount;
			V v = remappingFunction.apply(key, oldValue);
			if (mc != modCount) { throw new ConcurrentModificationException(); }
			if (v != null) {
				e.value = v;
				afterNodeAccess(e);
				return v;
			}
			else
				removeNode(hash, key, null, false, true);
		}
		return null;
	}

3.31 compute(K key, BiFunction remappingFunction)

查找key对应的value,如果key不在map中,则value为null,并以这两个参数调用remappingFunction,函数返回值为v。如果之前key不在map中而v不为null,就插入key和v。如果key在map中:如果v为null,就删除key;如果v不为null,则修改value为v。其间还有并发检查和访问插入钩子函数用于实现LRUCache

    @Override
	public V compute(K key,
					BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
		if (remappingFunction == null)
			throw new NullPointerException();
		int hash = hash(key);
		Node<K,V>[] tab; Node<K,V> first; int n, i;
		int binCount = 0;
		TreeNode<K,V> t = null;
		Node<K,V> old = null;
		if (size > threshold || (tab = table) == null ||
			(n = tab.length) == 0)//常规table检查
			n = (tab = resize()).length;
		if ((first = tab[i = (n - 1) & hash]) != null) {//判断hash相应链表是否存在,然后寻找节点
			if (first instanceof TreeNode)
				old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
			else {
				Node<K,V> e = first; K k;
				do {
					if (e.hash == hash &&
						((k = e.key) == key || (key != null && key.equals(k)))) {
						old = e;
						break;
					}
					++binCount;
				} while ((e = e.next) != null);
			}
		}
		V oldValue = (old == null) ? null : old.value;
		int mc = modCount;
		V v = remappingFunction.apply(key, oldValue);//调用函数
		if (mc != modCount) { throw new ConcurrentModificationException(); }
		if (old != null) {
			if (v != null) {
				old.value = v;
				afterNodeAccess(old);
			}
			else
				removeNode(hash, key, null, false, true);
		}
		else if (v != null) {
			if (t != null)
				t.putTreeVal(this, tab, hash, key, v);
			else {
				tab[i] = newNode(hash, key, v, first);
				if (binCount >= TREEIFY_THRESHOLD - 1)
					treeifyBin(tab, hash);
			}
			modCount = mc + 1;
			++size;
			afterNodeInsertion(true);
		}
		return v;
	}

3.32 merge(K key, V value, BiFunction remappingFunction)

查找key,如果key存在:map中key对应的value如果不为null,就调用remappingFunction,返回值为v,如果返回值为null且调用merge的value为null,则从map中删除key;否则用remappingFunction的返回值插入value;如果调用merge的value不为null,插入value。

@Override
public V merge(K key, V value,
			BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
	if (value == null)
		throw new NullPointerException();
	if (remappingFunction == null)
		throw new NullPointerException();
	int hash = hash(key);
	Node<K,V>[] tab; Node<K,V> first; int n, i;
	int binCount = 0;
	TreeNode<K,V> t = null;
	Node<K,V> old = null;
	if (size > threshold || (tab = table) == null ||
		(n = tab.length) == 0)//常规table检查
		n = (tab = resize()).length;
	if ((first = tab[i = (n - 1) & hash]) != null) {//检查hash后table中是否存在相应链表,然后寻找key
		if (first instanceof TreeNode)
			old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
		else {
			Node<K,V> e = first; K k;
			do {
				if (e.hash == hash &&
					((k = e.key) == key || (key != null && key.equals(k)))) {
					old = e;
					break;
				}
				++binCount;
			} while ((e = e.next) != null);
		}
	}
	if (old != null) {
		V v;
		if (old.value != null) {
			int mc = modCount;
			v = remappingFunction.apply(old.value, value);
			if (mc != modCount) {
				throw new ConcurrentModificationException();
			}
		} else {
			v = value;
		}
		if (v != null) {
			old.value = v;
			afterNodeAccess(old);
		}
		else
			removeNode(hash, key, null, false, true);
		return v;
	}
	if (value != null) {
		if (t != null)
			t.putTreeVal(this, tab, hash, key, value);
		else {
			tab[i] = newNode(hash, key, value, first);
			if (binCount >= TREEIFY_THRESHOLD - 1)
				treeifyBin(tab, hash);
		}
		++modCount;
		++size;
		afterNodeInsertion(true);
	}
	return value;
}

3.33 forEach(BiConsumer action)

进行基本合法性检查,然后调用foreach循环调用actioin

@Override
public void forEach(BiConsumer<? super K, ? super V> action) {
	Node<K,V>[] tab;
	if (action == null)
		throw new NullPointerException();
	if (size > 0 && (tab = table) != null) {
		int mc = modCount;
		for (Node<K,V> e : tab) {
			for (; e != null; e = e.next)
				action.accept(e.key, e.value);
		}
		if (modCount != mc)
			throw new ConcurrentModificationException();
	}
}

3.34 replaceAll(BiFunction function)

进行基本合法性检查,然后foreach循环调用action并将返回值赋给value

@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
	Node<K,V>[] tab;
	if (function == null)
		throw new NullPointerException();
	if (size > 0 && (tab = table) != null) {
		int mc = modCount;
		for (Node<K,V> e : tab) {
			for (; e != null; e = e.next) {
				e.value = function.apply(e.key, e.value);
			}
		}
		if (modCount != mc)
			throw new ConcurrentModificationException();
	}
}

3.35 clone()

重新初始化,然后把当前HashMap插入到克隆的HashMap中

@Override
public Object clone() {
	HashMap<K,V> result;
	try {
		result = (HashMap<K,V>)super.clone();
	} catch (CloneNotSupportedException e) {
		// this shouldn't happen, since we are Cloneable
		throw new InternalError(e);
	}
	result.reinitialize();
	result.putMapEntries(this, false);
	return result;
}

3.36 loadFactor()

直接返回loadFactor

final float loadFactor() { return loadFactor; }

3.37 capacity()

如果table不为null,返回table的长度,否则如果threshold大于0,返回threshol,否则返回16(即DEFAULT_INITIAL_CAPACITY)

final int capacity() {
	return (table != null) ? table.length :
		(threshold > 0) ? threshold :
		DEFAULT_INITIAL_CAPACITY;
}

3.38 writeObject(java.io.ObjectOutputStream s)

通过ObjectOutputStream先写入非static和非transient的变量(即threshold和loadFactor),然后写入capacity,size,最后调用internalWriteEntries()写入table中的键值对

	private void writeObject(java.io.ObjectOutputStream s)
		throws IOException {
		int buckets = capacity();
		// Write out the threshold, loadfactor, and any hidden stuff
		s.defaultWriteObject();
		s.writeInt(buckets);
		s.writeInt(size);
		internalWriteEntries(s);
	}

3.39 readObject(java.io.ObjectInputStream s)

按照writeObject相反的顺序读出变量,但是capacity会被丢弃重新计算出来。最后键值对读出来再putVal进去

private void readObject(java.io.ObjectInputStream s)
		throws IOException, ClassNotFoundException {
		// Read in the threshold (ignored), loadfactor, and any hidden stuff
		s.defaultReadObject();
		reinitialize();
		if (loadFactor <= 0 || Float.isNaN(loadFactor))
			throw new InvalidObjectException("Illegal load factor: " +
											loadFactor);
		s.readInt();                // Read and ignore number of buckets
		int mappings = s.readInt(); // Read number of mappings (size)
		if (mappings < 0)
			throw new InvalidObjectException("Illegal mappings count: " +
											mappings);
		else if (mappings > 0) { // (if zero, use defaults)
			// Size the table using given load factor only if within
			// range of 0.25...4.0
			float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);
			float fc = (float)mappings / lf + 1.0f;
			int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
					DEFAULT_INITIAL_CAPACITY :
					(fc >= MAXIMUM_CAPACITY) ?
					MAXIMUM_CAPACITY :
					tableSizeFor((int)fc));
			float ft = (float)cap * lf;
			threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
						(int)ft : Integer.MAX_VALUE);

			// Check Map.Entry[].class since it's the nearest public type to
			// what we're actually creating.
			SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Map.Entry[].class, cap);
			@SuppressWarnings({"rawtypes","unchecked"})
			Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
			table = tab;

			// Read the keys and values, and put the mappings in the HashMap
			for (int i = 0; i < mappings; i++) {
				@SuppressWarnings("unchecked")
					K key = (K) s.readObject();
				@SuppressWarnings("unchecked")
					V value = (V) s.readObject();
				putVal(hash(key), key, value, false, false);
			}
		}
	}

3.40 newNode(int hash, K key, V value, Node next)
显然

Node<K,V> newNode(int hash, K key, V value, Node<K,V> next) {
	return new Node<>(hash, key, value, next);
}

3.41 replacementNode(Node < K,V > p, Node < K,V > next)

显然

Node<K,V> replacementNode(Node<K,V> p, Node<K,V> next) {
	return new Node<>(p.hash, p.key, p.value, next);
}

3.42 newTreeNode(int hash, K key, V value, Node < K,V > next)

显然

TreeNode<K,V> newTreeNode(int hash, K key, V value, Node<K,V> next) {
	return new TreeNode<>(hash, key, value, next);
}

3.43 replacementTreeNode(Node < K,V > p, Node < K,V > next)

显然

TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) {
	return new TreeNode<>(p.hash, p.key, p.value, next);
}

3.44 reinitialize()

将除了threshold和loadFactor之外的变量全部初始化

void reinitialize() {
	table = null;
	entrySet = null;
	keySet = null;
	values = null;
	modCount = 0;
	threshold = 0;
	size = 0;
}

3.45 afterNodeAccess(Node < K,V > p)

给子类(LinkedHashMap)的钩子

void afterNodeAccess(Node<K,V> p) { }

3.46 afterNodeInsertion(boolean evict)

给子类(LinkedHashMap)的钩子

void afterNodeInsertion(boolean evict) { }

3.47 afterNodeRemoval(Node < K,V > p)

给子类(LinkedHashMap)的钩子

void afterNodeRemoval(Node<K,V> p) { }

3.48 internalWriteEntries(java.io.ObjectOutputStream s)

双重循环遍历所有key同时写入key value至序列化流中。

	void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException {
		Node<K,V>[] tab;
		if (size > 0 && (tab = table) != null) {
			for (Node<K,V> e : tab) {
				for (; e != null; e = e.next) {
					s.writeObject(e.key);
					s.writeObject(e.value);
				}
			}
		}
	}

4. HashMap的内部类

这里就介绍下Node,KeySet,ValueSet和TreeNode,其它的暂且略去,也比较易懂

4.1 Node

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的成员变量包括:

    final int hash;
    final K key;
    V value;
    Node<K,V> next;

成员函数基本很明朗,就不怎么解释了

4.2 KeySet

final class KeySet extends AbstractSet<K> {
        public final int size()                 { return size; }
        public final void clear()               { HashMap.this.clear(); }
        public final Iterator<K> iterator()     { return new KeyIterator(); }
        public final boolean contains(Object o) { return containsKey(o); }
        public final boolean remove(Object key) {
            return removeNode(hash(key), key, null, false, true) != null;
        }
        public final Spliterator<K> spliterator() {
            return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
        }
        public final void forEach(Consumer<? super K> action) {
            Node<K,V>[] tab;
            if (action == null)
                throw new NullPointerException();
            if (size > 0 && (tab = table) != null) {
                int mc = modCount;
                for (Node<K,V> e : tab) {
                    for (; e != null; e = e.next)
                        action.accept(e.key);
                }
                if (modCount != mc)
                    throw new ConcurrentModificationException();
            }
        }
    }

可以看到,KeySet是通过将大部分方法委托给其它类(主要是HashMap)来实现的,foreach例外。创建KeySet没有任何开销(就跟new Object())一样。

4.3 EntrySet

final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
        public final int size()                 { return size; }
        public final void clear()               { HashMap.this.clear(); }
        public final Iterator<Map.Entry<K,V>> iterator() {
            return new EntryIterator();
        }
        public final boolean contains(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
            Object key = e.getKey();
            Node<K,V> candidate = getNode(hash(key), key);
            return candidate != null && candidate.equals(e);
        }
        public final boolean remove(Object o) {
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>) o;
                Object key = e.getKey();
                Object value = e.getValue();
                return removeNode(hash(key), key, value, true, true) != null;
            }
            return false;
        }
        public final Spliterator<Map.Entry<K,V>> spliterator() {
            return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0);
        }
        public final void forEach(Consumer<? super Map.Entry<K,V>> action) {
            Node<K,V>[] tab;
            if (action == null)
                throw new NullPointerException();
            if (size > 0 && (tab = table) != null) {
                int mc = modCount;
                for (Node<K,V> e : tab) {
                    for (; e != null; e = e.next)
                        action.accept(e);
                }
                if (modCount != mc)
                    throw new ConcurrentModificationException();
            }
        }
    }

可以看到,EntrySet是通过将大部分方法委托给其它类(主要是HashMap)来实现的,foreach例外。创建EntrySet没有任何开销(就跟new Object())一样。

4.4 TreeNode

由于源码太长,就不贴上来了。TreeNode主要实现了红黑树的所有操作,对于红黑树的原理,建议查找相关资料。

我们先看下TreeNode的成员变量有哪些:

        //继承自Node
        final int hash;
        final K key;
        V value;
        Node<K,V> next;
		//继承自LinkedHashMap.Entry
        Entry<K,V> before, after;
		//自带
		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

可见,采用TreeNode在使用一般64位JVM(引用大小为8位),则TreeNode的大小是Node的2.7倍。不过现在服务器内存越来越大,用内存换时间(从O(n)->O(logN))还是划得来的。




后记

关于利用HashMap中留下的钩子函数,其实是给LinkedHashMap用于实现LRUCache的,如何实现请看这篇文章:如何设计实现一个LRU Cache?



参考资料

  1. 阿里二面准备(Java 研发)
  2. 基础知识(一) HashMap 源码详解
  3. HashMap的扩容及树化过程
  4. HashMap.comparableClassFor(Object x)方法解读




你可能感兴趣的:(Java,算法/数据结构)