1.前置知识

大家都知道HashMap采用的是数组+链表的方式进行数据的存储的,链表中的每一个结点是一个(1.7)Entry或(1.8)Node对象,这两个对象除了对象名称以外区别不是很大,我们来看一看这个HashMap的静态内部类的组成吧.
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash; //经过对key的hashcode进行rehash后生成的新的hash值,保证存储均匀
}
HashMap中也存在这样的几个变量,size:当前的HashMap中已经存储的值键对数
threshold=loadFactor*size 阈值达到多少时执行扩容操作
2.HashMap1.7中常用操作
put(K key, V value)
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);//首先这个方法会先从第0个桶中获取该桶元素的头节点,然后遍历查看是否已经存在这样key为空的Entry对象,如果有替换然后返回原来的Val,否则进行addEntry操作
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);//h & (length-1)相当于hashcode对%length
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
/**这里说下为什么重写equals方法后要重写hashcode方法,==是比较两个对象的首地址是否
相等,equals中默认使用了==来比较两个对象,hashcode是对象的内存地址的一个hash值(不能保证不同的
对象hashcode一定不同,因为存在hash冲突,但是能够保证相同的对象hashcode一定相同),所以这里也采用
了这种优化.如果重写了equals方法不重写hashcode时,两个equals逻辑中相等的key在e.hash(entry的hash字段也是hash(key.hahsCode())) == hash就
不满足,自然不能够将旧的value值覆盖(例如存在两个String对象String a = new String("hello") ,
String b = new String("hello"); 假如这两个对象重写后equals相等,但是没有修改hashcode,因为这
两个对象的hashcode很大可能hashCode不相同,所以执行如
map.push(a,"world1")),map.push(b,"world2")) , 我们所希望的是最后往map中插入key:hello
value:world2 这样的一个entry但是结果却是不确定的,若这两个对象hash冲突时候最后结果为一个
Entry,但是大多数没有冲突的情况下两个Entry,这样会照成一个Java中和Hash算法的集合类无法使用的结
果 **/
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
put后随即引入了addEntry操作
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操作主要做了一些进行重新hash定位前后的一些操作,比如判断传入的参数是不是过大和得到转换后的新Entry[]桶后将其替换原来的Entry桶,转换的核心主要还是在transfer方法中
/**
* Transfers all entries from current table to newTable.
*/
void transfer(Entry[] newTable) {
Entry[] src = table;
int newCapacity = newTable.length;
for (int j = 0; j < src.length; j++) {
Entry<K,V> e = src[j];
if (e != null) {
src[j] = null;
do { //这里采取的是对每个Entry进行一个重新定位然后头插法插入
Entry<K,V> next = e.next;
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
} while (e != null);
}
}
}
V get(Object key)
public V get(Object key) {
if (key == null)
//在table[0]中遍历所有的entry找到其中key为null的Entry并返回他的value
return getForNullKey();
int hash = hash(key.hashCode());
//然后用indexFor(hash,table.length)方法找到理论上该值键对在table中位置,然后遍历该桶下面的所有
entry
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.equals(k)))
return e.value;
}
return null;
}
2.HashMap1.8中常用操作
put(K key, V value)
final V putVal(int hash(hash(key)), K key, V value, boolean onlyIfAbsent
//(仅仅在key不存在的时候执行添加,而不覆盖原有的value),
boolean evict (LinkedHashMap移除机制,这里用不到)) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length; //table为空的时候会触发resize()操作 table为空时 这里会
//初始化一些参数capacity和threshold 然后创建table数组
if ((p = tab[i = (n - 1) & hash]) == null) //该桶中没有数据则直接创建一个结点使这个结点放入桶中
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)))) //这里查看桶中链表的头
//节点或者红黑树的根结点是不是和当前传入的相同的话,将相同的Node结点赋给Node e
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) {//如果在该桶中的列表中没有发生Key冲突,
//采用尾插法插入结点,如果当前桶中的链表达到了TREEIFY_THRESHOLD这个阙值则发生树化
//*******************************
//这里可以看到插入方式换成了尾插法
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))))
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)
//rehash操作
resize();
afterNodeInsertion(evict);
return null;
}
1.8resize()
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
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) {
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;
/*********************************************************
这里的操作就是 (e.hash & oldCap) == 0 这一句,
这一句如果是true,表明(e.hash & (newCap - 1))还会和
e.hash & (oldCap - 1)结果一样。因为oldCap和newCap是2的次幂,
并且newCap是oldCap的两倍,就相当于oldCap的唯一
一个二进制的1向高位移动了一位(e.hash & oldCap) == 0
就代表了(e.hash & (newCap - 1))还会和e.hash & (oldCap - 1)一样。
*************************************************/
//如果扩容后元素的位置没发生变化 lo是原来同桶位置中的链表的首尾指针,
//hi指的是新的桶中链表的首尾指针
if ((e.hash & oldCap) == 0) {
//这里首先判断低位桶中链表的尾指针是不是存在,如果不存在,说明链表中没有数据,
//则将头尾指针分别指向e,否则的化地位桶链表中已经有数据了,
//采用尾插法插入然后然后重置尾指针的指向.
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);
//将lo和hi分别指向对应的桶
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
get核心方法->getNode(int hash, Object key)
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;
}
4.我认为HashMap1.7和1.8的主要区别在
- 头插法到尾插法
- 新增加了红黑树
- 对resize()操作中迁移寻找在新桶中的位置做了优化
HashMap在1.7和1.8的主要区别体现在内部实现上,1.8引入了红黑树优化,从头插法变为尾插法,以及resize()操作的优化。1.7中,resize时主要操作在transfer方法中,1.8的resize策略更加高效。
1万+

被折叠的 条评论
为什么被折叠?



