如下针对JDK 1.8版本中的HashMap进行分析。html
概述
哈希表基于Map
接口的实现。此实现提供了全部可选的映射操做,而且容许键为null
,值也为null
。HashMap 除了不支持同步操做以及支持null
的键值外,其功能大体等同于 Hashtable。这个类不保证元素的顺序,而且也不保证随着时间的推移,元素的顺序不会改变。java
假设散列函数使得元素在哈希桶中分布均匀,那么这个实现对于 put 和 get 等操做提供了常数时间的性能。node
对于一个 HashMap 的实例,有两个因子影响着其性能:初始容量和负载因子。容量就是哈希表中哈希桶的个数,初始容量就是哈希表被初次建立时的容量大小。负载因子是在进行自动扩容以前衡量哈希表存储键值对的一个指标。当哈希表中的键值对超过capacity * loadfactor
时,就会进行 resize 的操做。数组
做为通常规则,默认负载因子(0.75)在时间和空间成本之间提供了良好的折衷。负载因子越大,空间开销越小,可是查找的开销变大了。并发
注意,迭代器的快速失败行为不能获得保证,通常来讲,存在非同步的并发修改时,不可能做出任何坚定的保证。快速失败迭代器尽最大努力抛出ConcurrentModificationException
异常。所以,编写依赖于此异常的程序的作法是错误的,正确作法是:迭代器的快速失败行为应该仅用于检测程序错误。app
源码分析
主要字段
/** * 初始容量大小 —— 必须是2的幂次方 */ static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 /** * 最大容量 */ static final int MAXIMUM_CAPACITY = 1 << 30; /** * 默认负载因子 */ static final float DEFAULT_LOAD_FACTOR = 0.75f; /** * 当链表长度超过这个值时转换为红黑树 */ static final int TREEIFY_THRESHOLD = 8; /** * 树形阈值,当小于这个值时,红黑树转换为链表 */ static final int UNTREEIFY_THRESHOLD = 6; /** * The smallest table capacity for which bins may be treeified. * (Otherwise the table is resized if too many nodes in a bin.) * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts * between resizing and treeification thresholds. */ static final int MIN_TREEIFY_CAPACITY = 64; /** * table 在第一次使用时进行初始化并在须要的时候从新调整自身大小。对于 table 的大小必须是2的幂次方。 */ transient Node<K,V>[] table; /** * Holds cached entrySet(). Note that AbstractMap fields are used * for keySet() and values(). */ transient Set<Map.Entry<K,V>> entrySet; /** * 键值对的个数 */ transient int size; /** * HashMap 进行结构性调整的次数。结构性调整指的是增长或者删除键值对等操做,注意对于更新某个键的值不是结构特性调整。 */ transient int modCount; /** * 所能容纳的 key-value 对的极限(表的大小 capacity * load factor),达到这个容量时进行扩容操做。 */ int threshold; /** * 负载因子,默认值为 0.75 */ final float loadFactor;
从上面咱们能够得知,HashMap中指定的哈希桶数组<u>table.length</u>必须是2的幂次方,这与常规性的把哈希桶数组设计为素数不同。指定为2的幂次方主要是在两方面作优化:函数
- 扩容:扩容的时候,哈希桶扩大为当前的两倍,所以只须要进行左移操做
- 取模:因为哈希桶的个数为2的幂次,所以能够用**&**操做来替代耗时的模运算,
n % table.length -> n & (table.length - 1)
哈希函数
/** * 哈希函数 */ static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }
key 的哈希值经过它自身hashCode的高十六位与低十六位进行亦或获得。这么作得缘由是由于,因为哈希表的大小固定为 2 的幂次方,那么某个 key 的 hashCode 值大于 table.length,其高位就不会参与到 hash 的计算(对于某个 key 其所在的桶的位置的计算为 hash & (table.length - 1)
)。所以经过hashCode()
的高16位异或低16位实现的:(h = key.hashCode()) ^ (h >>> 16)
,主要是从速度、功效、质量来考虑的,保证了高位 Bits 也能参与到 Hash 的计算。源码分析
tableSizeFor函数
/** * 返回大于等于capacity的最小2的整数次幂 */ static final int tableSizeFor(int cap) { int n = cap - 1; n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; }
根据注释能够知道,这个函数返回大于或等于cap的最小二的整数次幂的值。好比对于3,返回4;对于10,返回16。详解以下: 假设对于n(32位数)其二进制为 01xx...xx, n >>> 1,进行无符号右移一位, 001xx..xx,位或得 011xx..xx n >>> 2,进行无符号右移两位, 00011xx..xx,位或得 01111xx..xx 依此类推,无符号右移四位再进行位或将获得8个1,无符号右移八位再进行位或将获得16个1,无符号右移十六位再进行位或将获得32个1。根据这个咱们能够知道进行这么屡次无符号右移及位或操做,那么可以让数n的二进制位最高位为1的后面的二进制位所有变成1。此时进行 +1 操做,便可获得最小二的整数次幂的值。(《高效程序的奥秘》第3章——2的幂界方 有对此进行进一步讨论,可自行查看) 回到上面的程序,之因此在开头先进行一次 -1 操做,是为了防止传入的cap自己就是二的幂次方,此时获得的就是下一个二的幂次方了,好比传入4,那么在不进行 -1 的状况下,将获得8。性能
构造函数
/** * 传入指定的初始容量和负载因子 */ 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; //返回2的幂次方 this.threshold = tableSizeFor(initialCapacity); }
对于上面的构造器,咱们须要注意的是this.threshold = tableSizeFor(initialCapacity);
这边的 threshold 为 2的幂次方,而不是capacity * load factor
,固然此处并不是是错误,由于此时 table 并无真正的被初始化,初始化动做被延迟到了putVal()
当中,因此 threshold 会被从新计算。优化
/** * 根据指定的容量以及默认负载因子(0.75)初始化一个空的 HashMap 实例 * * 若是 initCapacity是负数,那么将抛出 IllegalArgumentException */ public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); } /** * 根据默认的容量和负载因子初始化一个空的 HashMap 实例 */ public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted } /** * Constructs a new <tt>HashMap</tt> with the same mappings as the * specified <tt>Map</tt>. The <tt>HashMap</tt> is created with * default load factor (0.75) and an initial capacity sufficient to * hold the mappings in the specified <tt>Map</tt>. * * @param m the map whose mappings are to be placed in this map * @throws NullPointerException if the specified map is null */ public HashMap(Map<? extends K, ? extends V> m) { this.loadFactor = DEFAULT_LOAD_FACTOR; putMapEntries(m, false); }
查询
/** * 返回指定 key 所对应的 value 值,当不存在指定的 key 时,返回 null。 * * 当返回 null 的时候并不代表哈希表中不存在这种关系的映射,有可能对于指定的 key,其对应的值就是 null。 * 所以能够经过 containsKey 来区分这两种状况。 */ public V get(Object key) { Node<K,V> e; return (e = getNode(hash(key), key)) == null ? null : e.value; } /** * 1.首先经过 key 的哈希值找到其所在的哈希桶 * 2.对于 key 所在的哈希桶只有一个元素,此时就是 key 对应的节点, * 3.对于 key 所在的哈希桶超过一个节点,此时分两种状况: * 若是这是一个 TreeNode,代表经过红黑树存储,在红黑树中查找 * 若是不是一个 TreeNode,代表经过链表存储(链地址法),在链表中查找 * 4.查找不到相应的 key,返回 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) { 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; }
<span id="存储">存储</span>
/** * 存储指定的批量的映射关系对 * @param m 指定的批量的映射关系对 * @param evict 判断是否移除,最初构造映射关系时为false,不然为 true(为 true 时会执行 afterNodeInsertion 函数) */ final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) { int s = m.size(); if (s > 0) { if (table == null) { // pre-size //计算须要的数组桶的大小 float ft = ((float)s / loadFactor) + 1.0F; int t = ((ft < (float)MAXIMUM_CAPACITY) ? (int)ft : MAXIMUM_CAPACITY); //超过所能容纳的 key-value 对的极限,置 threshold 为2的幂次方,在 resize 的时候会用到 //因为此时哈希表为 null,所以在存储的时候必定会执行 resize,具体可看 putVal 函数 if (t > threshold) threshold = tableSizeFor(t); //存储的映射关系对的数量大于所能容纳的 key-value 对的极限时,扩容 } else if (s > threshold) resize(); 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); } } } /** * 在映射中,将指定的键与指定的值相关联。若是映射关系以前已经有指定的键,那么旧值就会被替换 */ public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } /** * * @param onlyIfAbsent if true, don't change existing value * @param evict 判断是否移除,为 false时,表示哈希表正在建立 * * 1.判断哈希表 table 是否为空,是的话进行扩容操做 * 2.根据键 key 计算获得的 哈希桶数组索引,若是 table[i] 为空,那么直接新建节点 * 3.判断 table[i] 的首个元素是否等于 key,若是是的话就更新旧的 value 值 * 4.判断 table[i] 是否为 TreeNode,是的话即为红黑树,直接在树中进行插入 * 5.遍历 table[i],遍历过程发现 key 已经存在,更新旧的 value 值,不然进行插入操做,插入后发现链表长度大于8,则将链表转换为红黑树 */ final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; //哈希表 table 为空,进行扩容操做 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; // tab[i] 为空,直接新建节点 if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); else { Node<K,V> e; K k; //tab[i] 首个元素即为 key,更新旧值 if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; //当前节点为 TreeNode,在红黑树中进行插入 else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); else { //遍历 tab[i],key 已经存在,更新旧的 value 值,不然进行插入操做,插入后链表长度大于8,将链表转换为红黑树 for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); //链表长度大于8 if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); break; } // key 已经存在 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } //key 已经存在,更新旧值 if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } //HashMap插入元素代表进行告终构性调整 ++modCount; //实际键值对数量超过 threshold,进行扩容操做 if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
扩容
/** * 初始化或者对哈希表进行扩容操做。若是当前哈希表为空,则根据字段阈值中的初始容量进行分配。 * 不然,由于咱们扩容两倍,那么对于桶中的元素要么在原位置,要么在原位置再移动2次幂的位置。 */ 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 //调用了HashMap的带参构造器,初始容量用threshold替换, //在带参构造器中,threshold的值为 tableSizeFor() 的返回值,也就是2的幂次方,而不是 capacity * load factor 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); } threshold = newThr; @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) { //将原哈希桶置空,以便GC oldTab[j] = null; //当前节点不是以链表形式存在,直接计算其应放置的新位置 if (e.next == null) newTab[e.hash & (newCap - 1)] = e; //当前节点是TreeNode 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); if (loTail != null) { loTail.next = null; newTab[j] = loHead; } if (hiTail != null) { hiTail.next = null; newTab[j + oldCap] = hiHead; } } } } } return newTab; }
由于哈希表使用2次幂的拓展(指长度拓展为原来的2倍),因此在扩容的时候,元素的位置要么在原位置,要么在原位置再移动2次幂的位置。为何是这么一个规律呢?咱们假设 n 为 table 的长度,图(a)表示扩容前的key1和key2两种key肯定索引位置的示例,图(b)表示扩容后key1和key2两种key肯定索引位置的示例,其中hash1是key1对应的哈希与高位运算结果。 元素在从新计算hash以后,由于n变为2倍,那么n-1的mask范围在高位多1bit(红色),所以新的index就会发生这样的变化:
所以,咱们在扩容的时候,只须要看看原来的hash值新增的那个 bit 是1仍是0就行了,是0的话索引没变,是1的话索引变成“原索引+oldCap”,能够看看下图为16扩充为32的resize示意图:
删除
/** * 删除指定的 key 的映射关系 */ public V remove(Object key) { Node<K,V> e; return (e = removeNode(hash(key), key, null, false, true)) == null ? null : e.value; } /**Java * * 1.根据 key 的哈希值在哈希桶中查找是否存在这个包含有这个 key 的节点 * 链表头节点是要查找的节点 * 节点是TreeNode,在红黑树中查找 * 在链表中进行查找 * 2.若是查找到对应的节点,进行删除操做 * 从红黑树中删除 * 将链表头节点删除 * 在链表中删除 * * @param hash key 的 hash 值 * @param key 指定的 key * @param value 当 matchhValue 为真时,则要匹配这个 value * @param matchValue 为真而且与 value 相等时进行删除 * @param movable if false do not move other nodes while removing * @return the node, or null if none */ 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; //链表头即为要删除的节点 if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) node = p; else if ((e = p.next) != null) { //节点为TreeNode,在红黑树中查找是否存在指定的key if (p instanceof TreeNode) node = ((TreeNode<K,V>)p).getTreeNode(hash, key); else { //在链表中查找是否存在指定的key 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)))) { //从红黑树中删除 if (node instanceof TreeNode) ((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; } /** * 删除全部的映射关系 */ public void clear() { Node<K,V>[] tab; modCount++; if ((tab = table) != null && size > 0) { size = 0; for (int i = 0; i < tab.length; ++i) //置 null 以便 GC tab[i] = null; } }
问题
- 对于
new HashMap(18)
,那么哈希桶数组的大小是多少 - HashMap 要求哈希桶数组的长度是2的幂次方,这么设计的目的是为何
- HashMap 什么时候对哈希桶数组开辟内存
- 哈希函数是如何设计的,这么设计的意图是什么
- HashMap 扩容的过程,扩容时候对 rehash 进行了什么优化