本文源码基于AndroidSDK 28里的代码分析,其它的也大同小异,由于SDK里的源码不能Debug,这里就自己创建一个MyHashMap来分析
基本使用
MyHashMap<String,String> maps = new MyHashMap<>();
maps.put("12","aaa");
maps.put("22","bbb");
maps.put("32","ccc");
maps.put("42","ccc");
maps.put("52","ccc");
maps.put("62","ccc");
maps.put("72","ccc");
maps.put("82","ccc");
maps.put("92","ccc");
maps.put("02","ccc");
maps.put("112","ccc");
maps.put("122","ccc");
maps.put("132","ccc");
maps.put("142","ccc");
maps.put("152","ccc");
maps.put("162","ccc");
String text1 = maps.put("172", "ccc");
Log.e(TAG,"text1: "+text1);
String text2 = maps.put("172", "ccc");
Log.e(TAG,"text2: "+text2);
运行上面代码输出如下
2019-11-21 15:17:33.773 21779-21779/com.ancely.fyw E/ancely->>>: text1: null
2019-11-21 15:17:33.773 21779-21779/com.ancely.fyw E/ancely->>>: text2: ccc
上面可以得出一个小结论是当添加key value 时如果之前添加过,那么就会返回oldKey
先来看下HashMap的一个最简单构造函数
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16: table数组的默认大小
transient Node<K, V>[] table;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
public MyHashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
}
上面有一个table数组,也就是说其实部也是由数组构成的
这里有一个DEFAULT_LOAD_FACTOR默认值是0.75f这个值是一个扩容因子,就是假如table初始值为16,size/table.lengh>0.75&&table.length>64的时候就要开始扩容.
所以当我们new一个HashMap的时候可以得出结论: 里面什么都没有做,只是初始化了一个默认扩容因子
当我们第一次执行put的时候
maps.put("12","aaa");
执行的是这个 put(K key, V value)--->这个 putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict)
public V put(K key, V value) {
int hash = hash(key);
return putVal(hash, key, value, false, true);
}
咱们来debug模式分析走式
看图说话
第一次添加的时候这个table肯定是空, tab.length肯定是0所以肯定走下面这段代码看下图
直接走的是resize()这个方法,他会返回一个Node数组,这个方法主要是返回新的数组,或者是扩容后的数组进方法里面瞧一瞧,这里先把扩容的代码去掉,简洁一点
final Node<K, V>[] resize() {
//第一次进来: 这个肯定为空
Node<K, V>[] oldTab = table;
//第一次进来: oldTab为空,这个值肯定为0
int oldCap = (oldTab == null) ? 0 : oldTab.length;
//第一次进来: 这个值也没有初始化也为0
int oldThr = threshold;
//第一次进来: 这二个值都为0
int newCap, newThr = 0;
//第一次进来: 上面分析了这个值为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
//第一次进来: oldThr这个值==0,pass
} else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
//第一次进来: 上面都不走肯定走这
else {
//第一次进来: 开始为下面二个参数赋值了
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int) (DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
//第一次进来: 给扩容因子赋值了,默认就是上面计算的12
threshold = newThr;
@SuppressWarnings({"rawtypes", "unchecked"})
Node<K, V>[] newTab = (Node<K, V>[]) new Node[newCap];
table = newTab;
return newTab;
}
上面代码就分析的很透彻了,这里上一个debug图
最后将这个newTab给返回出来
再回到之前代码
然后就开为创建Node对象,这个时候table容量就是16,第一个对象也给了值,由于是第一次添加最后走下去会返回一个null
由此第一次添加值就完成,当第二次....第十一次的时候,其实跟第一次也差不多,只是不会走第一个resize()方法,不需要重新创建table对象,当size大于我们之前的扩容因子的时候,这时就会重新来到resize()方法里,如下图
接下来就是走扩容的方法
他是怎么扩容的这就不分析了,完整代码如下.
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;
}
}
}
}
}
当我们添加同一个key的时候,会什么会替换原来的重而保持key唯一性,看下图分析
所以我们运行上面代码的时候,put有时会返回Null,有时会有值,接下来分析一下get,获取数据
来操作一把,添加下面代码,现运行,输出
String text1 = maps.put("172", "ccc");
Log.e(TAG,"text1: "+text1);
String text2 = maps.put("172", "ccc111");
Log.e(TAG,"text2: "+text2);
String getTest = maps.get("172");
Log.e(TAG,"getTest: "+getTest);
2019-11-21 16:42:41.213 24700-24700/com.ancely.fyw E/ancely->>>: text1: null
2019-11-21 16:42:41.213 24700-24700/com.ancely.fyw E/ancely->>>: text2: ccc
2019-11-21 16:42:41.213 24700-24700/com.ancely.fyw E/ancely->>>: getTest: ccc111
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;
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)//这个是LinkHashMap里面运用到的,不用理会
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;
}
再来看看debug
至此,HashMap的put,和get就分析完.
总结: HashMap里就是由一个Node<K,V>数组构成,默认容量为16,当size/table.length>0.75时并且table.length>64,就开始扩容,,扩容后的大小为table.length*2,
当我们put的时候,就会通hash值 key equals判断是不是数组里有,有则取出来替换,没有则创建Node对象,在put的时候,如果超出了扩容因子的话,就会执行resize方法重新创建新的Node数组对象,大小为之前的2倍,并重新对key进行计算存放到相应的bucket里.由于在put的时候hash有可能相同,这个时候就会找到table值下标对应的Node对象,并将这个hash相同的对象赋值给next 也就是单向链表
还有一点,如果单向链表>8的时候就会将单向链表转为红黑对,红黑数小于6的时候重新变为单向链表
在get的时候,也是通过length-1)&hash值来换取下标,如果取出下标不为空就会返回, 当然这里也有put相同的情况 ,就是hash值如果相同,就是取next如果还相同就一直next. 最终返回value值.
最后再来分析一下HashMap能不能把null当作key
maps.put(null,null);
来看看debug的走势
所以得出结论是Hashmap的key是可以Null的