1.说明,这里主要对jdk1.8的HashMap的get方法进行源码分析
2.本文章基于上一篇《Jdk1.8 HashMap中put()方法源码分析》 添加链接描述
3.测试源码贴这里
package com.yuhl.map;
import java.util.HashMap;
/**
* @author yuhl
* @Date 2020/10/19 23:02
* @Classname HashMap
* @Description 剖析HashMap源码
*/
public class HashMapDemp {
public static void main(String[] args) {
HashMap<String, String> mapMine = new HashMap<String, String>(15,0.7f);
mapMine.put("洛阳1", "洛阳洛阳1");
mapMine.put("洛阳2", "洛阳洛阳2");
mapMine.put("洛阳3", "洛阳洛阳3");
mapMine.put("洛阳4", "洛阳洛阳4");
mapMine.put("洛阳5", "洛阳洛阳5");
mapMine.put("洛阳6", "洛阳洛阳6");
mapMine.put("洛阳7", "洛阳洛阳7");
mapMine.put("洛阳8", "洛阳洛阳8");
mapMine.put("洛阳9", "洛阳洛阳9");
mapMine.put("洛阳10", "洛阳洛阳10");
mapMine.put("洛阳11", "洛阳洛阳11");
mapMine.put("洛阳12", "洛阳洛阳12");
mapMine.put("洛阳13", "洛阳洛阳13");
mapMine.put("洛阳14", "洛阳洛阳14");
mapMine.put("洛阳15", "洛阳洛阳15");
mapMine.put("洛阳16", "洛阳洛阳16");
mapMine.put("洛阳17", "洛阳洛阳17");
mapMine.get("洛阳1");
}
}
4.对mapMine.get(“洛阳1”);这句进行源码解析。
先把目前代码执行到这句前的状态图贴这里:
特别注意
要找的值在这里哦。
开始跟踪源码:
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
特别注意这句:
e = getNode(hash(key), key)
现调用:
hash(key)
即:hash(“洛阳1”)
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
此方法返回一个hash=28033810,和之前put()是的hash值一样。
调用此方法:
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;
}
获得hash所在位置上的第一个node,如果第一个不是就拿第一个的next,拿到第二个,如果是树化就会查询红黑树,此例子中:
first = tab[(n - 1) & hash]
中
(n - 1) & hash是18,直接返回table中的第一个结点。
然后调用:
e.value
获得:洛阳洛阳1
get方法比较简单。
总结:源码没有那么复杂,你必须攻克下来一部分源码,在此基础上再看其他源码就容易点。战役早点打。多跌几个跟头是好事!