Java源码分析之HashMap
说明:这里分析的是jdk1.8的源码。
1.位于java.util包下,从JDK1.2开始有的。
2.继承关系
<span style="font-family:Microsoft YaHei;font-size:12px;"> public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {}</span>
1)继承自抽象基类AbstractMap类
<span style="font-family:Microsoft YaHei;"> public abstract class AbstractMap<K,V> implements Map<K,V> {}</span>
2)实现Map接口
3)实现Cloneable、Serializable 接口,两个都是标记接口,前一个表示可以clone,后一个表示可以序列化
3.属性
<span style="font-family:Microsoft YaHei;"> <span style="font-size: 12px;"> /**
* Node类型的数组,不会序列化,只有当第一次使用的时候才会初始化,当需要的时候会重新分配大小,大小为原先的两倍。
* 数组的大小必须的是2的整数次幂,这里可以分析下是为什么?首先如果length是2的整数次幂的话,h&(length-1)将相当于对
*length取模,这样保证了散列的均匀;再者是length为2的整数次幂,也就是为偶数,这样length-1也就是奇数,那么h&(length-1)
*的值可能为奇数,也可能为偶数,这样保证了散列的均匀性,比较合理,如果length为奇数,那么length-1为偶数,那么h&(length-1)
*的值一定为偶数,这样会浪费一半的空间。
*/
transient Node<K,V>[] table;
transient Set<Map.Entry<K,V>> entrySet; //不会序列化
transient int size; //Map中元素的个数,不会序列化
transient int modCount; //改变结构的次数,不会序列化
int threshold; //需要跳转的极限值(容量*装载因子)
final float loadFactor; //装载因子</span></span>
4.构造函数
1)
<span style="font-family:Microsoft YaHei;"> public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY; //最大只能有 MAXIMUM_CAPACITY容量的Map对象
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity);
}</span>
两个参数,一个是初始化的大小,一个是装载因子。
2)
<span style="font-family:Microsoft YaHei;"> public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}</span>
一个参数,初始化的大小,使用默认的装载因子来创建,调用HashMap(int initialCapacity, float loadFactor)构造方法。
3)
<span style="font-family:Microsoft YaHei;"> public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}</span>
无参的构造方法,使用默认的装载因子。
4)
<span style="font-family:Microsoft YaHei;"> public HashMap(Map<? extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
putMapEntries(m, false);
}</span>
参数为Map类型的对象,使用putMapEntries()方法来添加对象。
5.内部类和方法
1.使用静态内部类表示Map中的结点元素
<span style="font-family:Microsoft YaHei;"><span style="font-size: 14px;"> </span><span style="font-size:12px;">static class Node<K,V> implements Map.Entry<K,V> {
final int hash; //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;
}
}</span></span>
2.静态的工具方法
1)
<span style="font-family:Microsoft YaHei;"> static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}</span>
hash()方法,返回对象的hashcode值。
2)<span style="font-family:Microsoft YaHei;"> <span style="font-weight: normal;">static Class<?> comparableClassFor(Object x) {
if (x instanceof Comparable) {
Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
if ((c = x.getClass()) == String.class) // bypass checks
return c; //如果x是String类型的返回String的Class对象
if ((ts = c.getGenericInterfaces()) != null) {
for (int i = 0; i < ts.length; ++i) {
if (((t = ts[i]) 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;
}</span></span>
<span style="font-family:Microsoft YaHei;"> <span style="font-weight: normal;">static Class<?> comparableClassFor(Object x) {
if (x instanceof Comparable) {
Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
if ((c = x.getClass()) == String.class) // bypass checks
return c; //如果x是String类型的返回String的Class对象
if ((ts = c.getGenericInterfaces()) != null) {
for (int i = 0; i < ts.length; ++i) {
if (((t = ts[i]) 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;
}</span></span>
返回对象x的Class对象,如果x的类实现了comparable接口,返回对象x的Class对象,否则返回空。
3)
<span style="font-family:Microsoft YaHei;"> static int compareComparables(Class<?> kc, Object k, Object x) {
return (x == null || x.getClass() != kc ? 0 :
((Comparable)k).compareTo(x));
}</span>
如果x为空或者x与k是同一类的对象返回0,否则返回k.compareTo(x)。
4)
<span style="font-family:Microsoft YaHei;">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;
}</span>
由于容量必须的2的整数次幂,所以传入构造函数中的容量参数,会进行一次操作,就会通过tableSizeFor()将参数转换为大于参数的最小的2的N次方数。
3 .其他方法
<span style="font-family:Microsoft YaHei;"> final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
int s = m.size();
if (s > 0) {
if (table == null) { // 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) //存入的元素大于下一次分配的大小
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);
}
}
}</span>
<span style="font-family:Microsoft YaHei;">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) //使用(n - 1) & hash的值作为数组的下标,如果当前的数组位置为空时
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)))) //判断每一个结点与k是否是同一个
break;
p = e;
}
}
if (e != null) { // 已经存在相同的键
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null) //如果允许改变已经存在的值或者老的值为空
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}</span>
<span style="font-family:Microsoft YaHei;">//往map中插值,默认是允许改变已存在的值
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}</span>
<span style="font-family:Microsoft YaHei;">//初始化或者将数组容量扩展为原来的两倍,如果原来为空,则按照默认的值进行初始化,如果不为空,则表明原来有元素,则将数组扩展为原来的两倍大小,在新数组中的元素要么呆在原来的位置,要么移到两倍index的位置。
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; // 原来的两倍
}
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) {//新的极限值为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) {
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) {
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;
}
}
}
}
}
return newTab;
}</span>
4.重写父接口Map中的default方法
由于在jdk1.8中加入了新特性,在接口可以有default方法。
<span style="font-family:Microsoft YaHei;">//通过键查找值,返回找到的值或者当没找到对应的key返回第二个参数
@Override
public V getOrDefault(Object key, V defaultValue) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? defaultValue : e.value;
}</span>
<span style="font-family:Microsoft YaHei;">//向hashmap中添加新的元素,但是不允许改变已存在的值。
@Override
public V putIfAbsent(K key, V value) {
return putVal(hash(key), key, value, true, true);
}</span>
<span style="font-family:Microsoft YaHei;">//删除某个已存在的键值对。
@Override
public boolean remove(Object key, Object value) {
return removeNode(hash(key), key, value, true, true) != null;
}</span>
<span style="font-family:Microsoft YaHei;">//将指定的已经存在的老的值替换成新的值,老的值需要用过参数传进来
@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;
}</span>
<span style="font-family:Microsoft YaHei;">//替换
@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;
}</span>
<span style="font-family:Microsoft YaHei;">//通过构建Java本地缓存,降低程序的计算量、程序复杂度。
//首先判断Map中是否存在指定的key的值,如果不存在,会自动调用mappingFunction(key)计算key的value,然后将key=value放入
//缓存map,如果mappingFunction(key)返回为null或者抛异常,则不会有记录存入map
@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;
}
}
V v = mappingFunction.apply(key);
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;
++size;
afterNodeInsertion(true);
return v;
}</span>
<span style="font-family:Microsoft YaHei;">//如果key已经存在,通过remappingFunction(key)重新计算。
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) {
V v = remappingFunction.apply(key, oldValue);
if (v != null) {
e.value = v;
afterNodeAccess(e);
return v;
}
else
removeNode(hash, key, null, false, true);
}
return null;
}</span>
<span style="font-family:Microsoft YaHei;">//通过remappingFunction()计算得到新的值,如果新的为空,且老的值不为空,则删除老的键值对,如果新的不为空,则插入新的键值对
@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)
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 = (old == null) ? null : old.value;
V v = remappingFunction.apply(key, oldValue);
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;
++size;
afterNodeInsertion(true);
}
return v;
}</span>
<span style="font-family:Microsoft YaHei;">//如果指定的key不存在或者value为空,则用给的value填上,否则用remappingFunction()计算得到结果替代。
//通常这个方法被用来组合多个value对于一个key时。
@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)
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);
}
}
if (old != null) {
V v;
if (old.value != null)
v = remappingFunction.apply(old.value, value);
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;
}</span>
<span style="font-family:Microsoft YaHei;">//jdk1.8新加入的
@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 (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next)
action.accept(e.key, e.value);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}</span>
<span style="font-family:Microsoft YaHei;">//jdk1.8新加入的
@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 (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next) {
e.value = function.apply(e.key, e.value);
}
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}</span>
6. jdk1.8中对HashMap的优化
如果数组中某个位置的链表长度过大的话,HashMap会动态的使用一个专门的TreeMap实现来替换它。这样做的结果会更好,是O(logn),而不是O(n)。那么它是如果运行的?前面造成的冲突的那些Key对应的结点只是简单的追加到对应链表的后面,这些记录只能通过遍历来进行炒作。但是如果超过某个阈值后HashMap开始将链表升级成一个二叉树,使用哈希值作为树的分支变量如果两个哈希值不等,但是指向同一个桶的话,较大的那个插入到右子树里。这样的由于链表变成一个二叉树,所以时间复杂度从O(n)-->O(logn)。
那么这么做有什么用?首先肯定是性能的提升,然后比如说有人恶意搞破坏,知道我们使用的hash算法,它可能会发送大量的请求,可能会导致严重的哈希碰撞,然后不断的访问key就会影响服务器的性能,这样就形成了一次拒绝服务攻击,jdk1.8对hashmap的优化可以有效的防止类似的攻击。