HashMap源码学习java8版
hashMap从7到8变化还是比较大,看下hashMap的源码
创建一个HashMap
HashMap map = new HashMap();
HashMap的结构
HashMap的一些常量
// 默认初始化容量 1左移4位,也就是16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
// 最大容量,1左移30位,也就是1073741824,大概10.7亿
static final int MAXIMUM_CAPACITY = 1 << 30;
// 默认装载因子0.75,这是对空间和时间的平衡,一般不要改
static final float DEFAULT_LOAD_FACTOR = 0.75f;
// 树化阈值,元素个数为8
static final int TREEIFY_THRESHOLD = 8;
// 非树化阈值,元素个数为6
static final int UNTREEIFY_THRESHOLD = 6;
// hashmap中的链表要变成树的最小元素数目阈值,64个
static final int MIN_TREEIFY_CAPACITY = 64;
HashMap内部的属性
// 1 hashmap简单说明是数组+链表(红黑树),充分发挥数组访问快,链表增删快的特性,table就是数组
transient Node<K,V>[] table;
// 2 hashMap转set后
transient Set<Map.Entry<K,V>> entrySet;
// 3 元素个数
transient int size;
// 4 修改次数
transient int modCount;
// 5 阈值
int threshold;
// 6 装载因子
final float loadFactor;
hashMap内部持有了一个接口Entry
interface Entry<K,V> {
K getKey();
V getValue();
V setValue(V value);
boolean equals(Object o);
int hashCode();
}
接口Entry的实现类就是静态内部类Node
static class Node<K,V> implements Map.Entry<K,V> {
// hash值
final int hash;
// key
final K key;
// value
V value;
// 改节点的后继,当hash值相同的时候,构成链表或红黑树会用到
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;
}
}
HashMap原理
hash原理
hashmap的hash方法,我们重点谈谈hash的原理。key.hashCode()获取的是int类型的hash值,范围是-2147483648到2147483648(负21.4亿到正21.4亿)。这么大的范围是很难发生hash碰撞的。hashMap开始的时候容量是16,也就是说数组是16,所以需要将hash值和hashMap的当前容量取模运算,hashmap中有如下代码。
int index = hash & (arrays.length-1);
例:arrays.length=16
那么arryas.length-1=15
对应的二进制数是1111,int是32位,其他位补0,得到:
00000000 00000000 00000000 00001111
假如hash值是17,表示成二进制数是:00000000 00000000 00000000 00010001
按位取余得到:
00000000 00000000 00000000 00001111
00000000 00000000 00000000 00010001
得到:
00000000 00000000 00000000 00000001,索引为1
如果我们的hashMap容量不是2的倍数比如是3,那么其二进制数据
是00000000 00000000 00000000 00000010,会发现索引为0的永远分配不到,造成无故浪费
static final int hash(Object key) {
int h;
// hashCode右移16位,正好是32bit的一半。
//与自己本身做异或操作(相同为0,不同为1)。
//就是为了混合哈希值的高位和地位,增加低位的随机性。
//并且混合后的值也变相保持了高位的特征。
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
public HashMap() {
// 设置了默认装载因子,什么也没做
this.loadFactor = DEFAULT_LOAD_FACTOR;
}
put()方法
public V put(K key, V value) {
//看看putVal
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;
// table为空
if ((tab = table) == null || (n = tab.length) == 0)
// 给tab分配容量
n = (tab = resize()).length;
//(n - 1) & hash计算hash位置,并且当前位置为空槽
if ((p = tab[i = (n - 1) & hash]) == null)
// 为空槽则构建新节点放入
tab[i] = newNode(hash, key, value, null);
else {
// 进入else说明hash到的槽位置有元素了,也就是说hash冲突了,我们继续来看看
Node<K,V> e; K k;
// hash到同一个位置有两种可能:1)是hash冲突,其实key并不相同;2)是更新key的值
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);
// 链长于大8转树判定
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
// 如果总容量小于64没必要转,扩展table;否则转树
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
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
// 有必要的话再扩容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
备注:put方法中,链表插值采用尾插法,与jdk7不同,7采用头插法;table为空扩容,添加元素结束后扩容。
扩容,resize()方法
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
// hashMap扩容前老容量
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// hashMap扩容前老阈值
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 {
// 默认给16容量
newCap = DEFAULT_INITIAL_CAPACITY;
// 默认给12
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"})
// 构建新的table,以为newCap作为容量
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;
// 找出扩容1倍后仍然在原桶中的节点元素
// 例如oldCap二进制10000,hash时采用&1111,则新增一倍后高位hascode也起散列作用
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
// 找出扩容1倍后不在原来桶中的节点元素
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;
}
// 原桶和新桶位置不同,差了刚好oldCap个单位,放入即可
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
get()方法
public V get(Object key) {
Node<K,V> e;
// 这里调用getNode
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;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash &&
// 找到返回即可
((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;
}
remove()方法
public V remove(Object key) {
Node<K,V> e;
// 这里调用removeNode方法
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
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) {
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)))) {
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;
}
拓展:HashSet原理
HashSet的实现原理是基于HashMap
// 创建一个HashSet
HashSet set = new HashSet();
public HashSet() {
// 这里在构造函数里面创建了HashMap
map = new HashMap<>();
}
// 类属性包括了一个HashMap
private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
hashSet的add()方法
public boolean add(E e) {
// 这里的add元素e,e其实是作为了map的key,因为hashmap的key不会重复,所以就保证了去重。PRESENT这个值是模拟的value值,不影响其他
return map.put(e, PRESENT)==null;
}