作者:海边的迷思萝_160 | 来源:互联网 | 2023-01-04 17:33
一点一点学源码
分析下面代码做了什么
Map map = new HashMap();
map.put("张三",10);
map.put("李四",20);
Integer age=map.get("张三");
1.初始化
接口 Map,Cloneable, Serializable
|
抽象类 AbstractMap
|
具体实现类 HashMap
Map map = new HashMap();调用无参构造,给全局final变量loadFactor赋初值 0.7
final float loadFactor;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
2,map.put("张三",10);
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//传入参数为 (774882,"张三",10,false,true)
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
/**
* Node{
* final int hash;
* final K key;
* V value;
* Node next;
* }
*/
Node[] tab;
Node p;
int n,i;
/**
*以上是该方法四个局部变量
*重点关注该方法对该实体类全局变量的修改
*该类中涉及到全局信息的有
* static final int TREEIFY_THRESHOLD = 8;
* transient Node[] table;
* transient int modCount;
* transient int size;
* int threshold;
*/
//全局变量table初始化时赋值null
if ((tab = table) == null || (n = tab.length) == 0)
//调用resize方法 n为16即tab数组初始化长度16 该方法执行时执行了 table = (Node[])new Node[16];
n = (tab = resize()).length;
//p=tab[2]==null
if ((p = tab[i = (n - 1) & hash]) == null)
//tab[2]=Node{hash:774882,key:"张三",value:10,next:null} 即table[2]=Node{hash:774882,key:"张三",value:10,next:null}
tab[i] = newNode(hash, key, value, null);
# else {//数组空间未被占用
# Node 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)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))))
# 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;//该值为1
if (++size > threshold)//size=1 threshold该值在resize方法中 threshold=16*0.75=12
resize();
afterNodeInsertion(evict); // Callbacks to allow LinkedHashMap post-actions 本类无方法体
return null;//张三 10被存储到table数组的第2位置中
}
2,map.put("李四",20);
//传入参数为 (842049,"李四",20,false,true)
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
/**
* Node{
* final int hash;
* final K key;
* V value;
* Node next;
* }
*/
Node[] tab;
Node p;
int n,i;
/**
*以上是该方法四个局部变量
*重点关注该方法对该实体类全局变量的修改
*该类中涉及到全局信息的有
* static final int TREEIFY_THRESHOLD = 8;
* transient Node[] table;
* transient int modCount;
* transient int size;
* int threshold;
*/
//此时tab={;;;table[2]=Node{hash:774882,key:"张三",value:10,next:null};;;} n=16
if ((tab = table) == null || (n = tab.length) == 0)
//不为null跳过初始化
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null) //tab[1]=null
//tab[1]=Node{hash:842049,key:"李四",value:20,next:null} 即table[1]=Node{hash:842049,key:"李四",value:20,next:null}
tab[i] = newNode(hash, key, value, null);
# else {//数组空间未被占用 下次查看该分支 先研究一下链表和树
# Node 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)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))))
# 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;//该值为2
if (++size > threshold)//size=2 threshold=16*0.75=12//下次查看该分支 超过阈值扩容
resize();
afterNodeInsertion(evict); // Callbacks to allow LinkedHashMap post-actions 本类无方法体
return null;//李四 20被存储到table数组的第1位置中
}
4,map.get("张三")
public V get(Object key) {
Node e;
//获取该位置的Node节点,直接获取该value值 返回10
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
//参数(774882,"张三")
final Node getNode(int hash, Object key) {
Node[] tab;
Node first, e;
int n;
K k;
/**
*table[1]=Node{hash:842049,key:"李四",value:20,next:null}
*table[2]=Node{hash:774882,key:"张三",value:10,next:null}
*/
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) { //first=table[1]=Node{;;;"张三"}
if (first.hash == hash &&
((k = first.key) == key || (key != null && key.equals(k))))
return first;//first.next==null 直接返回该Node 该位置无链表或树
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode)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;
}
简单理解即数据被存储到HashMap的
transient Node[ ] table; 中了Node继承了Entry维护key,value键值对,查询时先获取Node然后获取value
未考虑Node中key相同 value不同的情况以及 table数组大小超过阈值扩容情况,下次查看