解析HashMap源码,面试必备技能之一
目标
一.类继承关系
二.HashMap属性
1.7JDK
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // 1
static final int MAXIMUM_CAPACITY = 1 << 30; //2
static final float DEFAULT_LOAD_FACTOR = 0.75f; //3
static final Entry<?,?>[] EMPTY_TABLE = {};
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;//4
transient int size; //5
int threshold; //6
final float loadFactor; //7
transient int modCount; //8
1.数组的默认容量
2.最大容量
3.默认负载因子大小
4.hashMap的数组
5.当前数组存储的数量的大小
6.数组的大小(可在构造函数中初始化)
7.负载因子.可在构造函数中进行初始化
8.HashMap在结构上修改的次数
1.8JDK
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; //9
transient Node<K,V>[] table; //10
和1.7的属性基本一样,不一样的是
9.判断是否使用红黑树
10.把1.7的Entry改成了Node
三.HashMap底层数据结构
1.7JDK
1.8JDK(数据结构做了优化)
四.HashMap源码解析get/put
1.7JDK中的Entry的定义
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
int hash;
/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
///.......
}
每一个entry包含三个属性
key:map存储的key
value:map存储的value
next:指向下一个Entry对象
Put方法源码解析
public V put(K key, V value) {
//1
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
//2
if (key == null)
return putForNullKey(value);
//3
int hash = hash(key);
//4
int i = indexFor(hash, table.length);
//5
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
//6
addEntry(hash, key, value, i);
return null;
}
}
get方法源码解析
public V get(Object key) {
//1
if (key == null)
return getForNullKey();
//2
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
final Entry<K,V> getEntry(Object key) {
//3
if (size == 0) {
return null;
}
//4
int hash = (key == null) ? 0 : hash(key);
//5
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
1.8JDK
put方法源码解析
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
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//2
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//3
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);
else {
//5
for (int binCount = 0; ; ++binCount) {
//6
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//7
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
//8
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//9
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
get方法源码解析
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
//1
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//2
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
//3
if ((e = first.next) != null) {
//4
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
//5
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}//6
return null;
}
5.HashMap面试必问
HashMap 中没有两个相同的 key
在put源码中,且有一段循环遍历就是为了防止存在相同的 key 值,若发现两个 hash 值(key)相同时,HashMap 的处理方式是用新 value 替换旧 value,这里并没有处理 key
为什么数组的长度是2的n次方呢
static int indexFor(int h, int length) {
return h & (length-1);
}
为了让数组的数据减少碰撞,就应该保持数据的均匀分布,一般均匀分布,我们会想到数组长度取模,但是由于取模的消耗较大就会使用上面的方式进行处理,因为数组的长度是2的n次方,所以h&(lenght-1)和取模的效果是一样的,
其中为什么是2的n次方,我们可以举个例子如下图
可以看到当数组长度是15的时候,计算的下表示有重复的,这样就会导致hash碰撞,形成链表,影响了查询值的效率,以及导致数据分布不均匀,因此当length=2^n的时候,会降低碰撞的概率,这样会使得数据分布均匀,以及提高查询性能。