(1) 从源码可知,HashMap类中有一个很是重要的字段,就是 Node[] table,即哈希桶数组,明显它是一个Node的数组。咱们先来看看Node是什么。html
static class Node implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node next;//指向链表的下一个node
Node(int hash, K key, V value, Node next) { ... }
public final K getKey(){ ... }
public final V getValue() { ... }
public final String toString() { ... }
public final int hashCode() { ... }
public final V setValue(V newValue) { ... }
public final boolean equals(Object o) { ... }
}复制代码
(2) HashMap就是使用哈希表来存储的。哈希表为解决冲突,能够采用开放地址法和链地址法等来解决问题,Java中HashMap采用了链地址法。java
链地址法,简单来讲,就是数组加链表的结合。在每一个数组元素上都有一个链表结构,当数据被Hash后,获得数组下标,把数据放在对应下标元素的链表上(具体内容下文会说到)。node
在理解Hash和扩容流程以前,咱们得先了解下HashMap的几个字段。从HashMap的默认构造函数源码可知,构造函数就是对下面几个字段进行初始化,源码以下:程序员
int threshold; // 所能容纳的key-value对极限
final float loadFactor; // 负载因子
int modCount;
int size;复制代码
size这个字段其实很好理解,就是HashMap中实际存在的键值对数量。注意和table的长度length、容纳最大键值对数量threshold的区别。算法
modCount字段主要用来记录HashMap内部结构发生变化的次数,主要用于迭代的快速失败。强调一点,内部结构发生变化指的是结构发生变化,例如put新键值对,可是某个key对应的value值被覆盖不属于结构变化。数组
这里存在一个问题,即便负载因子和Hash算法设计的再合理,也免不了会出现拉链过长的状况,一旦出现拉链过长,则会严重影响HashMap的性能。因而,在JDK1.8版本中,对数据结构作了进一步的优化,引入了红黑树。而当链表长度太长(默认超过8)时,链表就转换为红黑树,利用红黑树快速增删改查的特色提升HashMap的性能,其中会用到红黑树的插入、删除、查找等算法。
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;
}复制代码
方法一:
static final int hash(Object key) { //jdk1.8 & jdk1.7
int h;
// h = key.hashCode() 为第一步 取hashCode值
// h ^ (h >>> 16) 为第二步 高位参与运算
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
方法二:
static int indexFor(int h, int length) {
//jdk1.7的源码,jdk1.8没有这个方法,原理是同样的只不过放到其余方法中去,例如put()
return h & (length-1); //第三步 取模运算
}复制代码
对于任意给定的对象,只要它的hashCode()返回值相同,那么程序调用方法一所计算获得的Hash码值老是相同的。咱们首先想到的就是把hash值对数组长度取模运算,这样一来,元素的分布相对来讲是比较均匀的。可是,模运算的消耗仍是比较大的,在HashMap中是这样作的:调用方法二来计算该对象应该保存在table数组的哪一个索引处。安全
这个方法很是巧妙,它经过h & (table.length -1)
来获得该对象的保存位,而HashMap底层数组的长度老是2的n次方,这是HashMap在速度上的优化。当length老是2的n次方时,h& (length-1)
运算等价于对length取模,也就是h%length
,可是&比%具备更高的效率。bash
x : 00010000
x-1 : 00001111
复制代码
y : 10110010
x-1 : 00001111
y&(x-1) : 00000010复制代码
y : 10110010
x : 00010000
y%x : 00000010复制代码
public V put(K key, V value) {
// 对key的hashCode()作hash
return 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;
// 步骤1:tab为空则建立
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// 步骤2:计算index,并对null作处理
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
// 步骤3:节点key存在,直接覆盖value
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
// 步骤4:判断该链为红黑树
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
// 步骤5:该链为链表
else {
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已经存在直接覆盖value
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;
// 步骤6:超过最大容量 就扩容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}复制代码
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}复制代码
void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
// 头插法,链表头部指向新的键值对
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
复制代码
和扩容相关的参数主要有:capacity、size、threshold 和 load_factor。数据结构
参数
|
含义
|
capacity
|
table 的容量大小,默认为 16。须要注意的是 capacity 必须保证为 2 的 n 次方。
|
size
|
table 的实际使用量。
|
threshold
|
size 的临界值,size 必须小于 threshold,若是大于等于,就必须进行扩容操做。
|
loadFactor
|
装载因子,table 可以使用的比例,threshold = capacity * loadFactor。
|
static final int DEFAULT_INITIAL_CAPACITY = 16;
static final int MAXIMUM_CAPACITY = 1 << 30;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
transient Entry[] table;
transient int size;
int threshold;
final float loadFactor;
transient int modCount;复制代码
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}复制代码
咱们分析下resize的源码,这里使用的是JDK1.7的代码,好理解一些,本质上区别不大。多线程
void resize(int newCapacity) { //传入新的容量
Entry[] oldTable = table; //引用扩容前的Entry数组
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) { //扩容前的数组大小若是已经达到最大(2^30)了
threshold = Integer.MAX_VALUE; //修改阈值为int的最大值(2^31-1),这样之后就不会扩容了
return;
}
Entry[] newTable = new Entry[newCapacity]; //初始化一个新的Entry数组
transfer(newTable); //将数据转移到新的Entry数组里
table = newTable; //HashMap的table属性引用新的Entry数组
threshold = (int)(newCapacity * loadFactor);//修改阈值
}
复制代码
void transfer(Entry[] newTable) {
Entry[] src = table; //src引用了旧的Entry数组
int newCapacity = newTable.length;
for (int j = 0; j < src.length; j++) { //遍历旧的Entry数组
Entry e = src[j]; //取得旧Entry数组的每一个元素
if (e != null) {
//释放旧Entry数组的对象引用(for循环后,旧的Entry数组再也不引用任何对象)
src[j] = null;
do {
Entry next = e.next;
//!!从新计算每一个元素在数组中的位置
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i]; //标记[1]
newTable[i] = e; //将元素放在数组上
e = next; //访问下一个Entry链上的元素
} while (e != null);
}
}
} 复制代码
其实上面的数组扩容、从新计算下标值并将旧数据插入到新数组的过程并不难,有意思的一点是JDK1.8在这里对扩容后的元素移动操做作了优化,具体过程以下。
通过观测能够发现,咱们使用的是2次幂的扩展(指长度扩为原来2倍),因此,元素的位置要么是在原位置,要么是在原位置再移动2次幂的位置。
看下图能够明白这句话的意思,n为table的长度,图(a)表示扩容前的key1和key2两种key肯定索引位置的示例,图(b)表示扩容后key1和key2两种key肯定索引位置的示例,其中hash1是key1对应的哈希与高位运算结果。
具体细节你们不妨能够去看看JDK1.8的源码中resize是怎么写的,这个思路的确十分巧妙。
public class HashMapInfiniteLoop {
private static HashMap map = new HashMap(2,0.75f);
public static void main(String[] args) {
map.put(5, "C");
new Thread("Thread1") {
public void run() {
map.put(7, "B");
System.out.println(map);
};
}.start();
new Thread("Thread2") {
public void run() {
map.put(3, "A);
System.out.println(map);
};
}.start();
}
} 复制代码
线程一被调度回来执行,先是执行 newTalbe[i] = e, 而后是e = next,致使了e指向了key(7),而下一次循环的next = e.next致使了next指向了key(3)。
阅读源码是一件十分耗费精力的事情,但从中你能够领悟到JDK做者的巧妙思路,在源码层面去理解为何HashMap是线程不安全的,HashMap的扩容机制等等,而不是仅仅停留在会用HashMap这个容器的表面理解上。