{
if(key ==
null) {
throw newNullPointerException(
"key == null");
}
V mapValue;
synchronized(
this) {
//现在hashMap中查找有没有这个key对应的节点(这个地方只要是get一次就会把命中的节点往首节点排)mapValue = map.get(key);
if(mapValue !=
null) {
//如果命中的话那么命中+1,返回该值hitCount++;
returnmapValue;
}
//如果没有命中的话那么没命中+1missCount++;
}
/*V createdValue = create(key);
if(createdValue ==
null) {
//如果不为没有命名的key创建新值,则直接返回;
}
// 接下来是如果用户重写了create方法后,可能会执行到(
this) {
createCount++;
//创建的数量增加mapValue = map.put(key, createdValue););
// 将刚刚创建的值放入map中,返回的值是在map中与key相对应的旧值(就是在放入new value前的old value)(mapValue !=
null) {
// There was a conflict so undo that last putmap.put(key, mapValue);
//如果不为空,说明不需要我们所创建的值,所以又把返回的值放进去}
else{
size += safeSizeOf(key, createdValue);
//为空,说明我们更新了这个key的值,需要重新计算大小}
}
if(mapValue !=
null) {
//上面放入的值有冲突entryRemoved(
false, key, createdValue, mapValue);
// 通知之前创建的值已经被移除,而改为mapValuemapValue;
}
else{
trimToSize(maxSize);
//没有冲突时,因为放入了新创建的值,大小已经有变化,所以需要修整大小createdValue;
}
}
来源: https://mp.weixin.qq.com/s?__biz=MzIwODk1NTYzOQ==&mid=2247483900&idx=1&sn=da41262deb010270100955ee7b3350fe&chksm=977a7064a00df972a14ff32a182d8c1b1f8bd7a819a57d715f864651dac72083542ee6505d36#rd