/**
* 该函数用于获取指定key的value
* @param key 传入的key
* @return 返回查找到的value对象,查找到则返回对象,没有则返回空
*/
public V get(Object key) {
HashMap.Node e;
return (e = this.getNode(hash(key), key)) == null ? null : e.value;
}
/**
*
* @param hash 需要被获取元素的hash值
* @param key 需要被获取的元素
* @return 返回被需要到的元素,没有获取到则返回null
*/
final HashMap.Node<K, V> getNode(int hash, Object key) {
//临时变量储存table数组
HashMap.Node[] tab;
//临时变量获取第一个元素
HashMap.Node first;
//n为table的长度
int n;
//如果table已经被初始化切table数组的长度大于0,在已知元素的查找位置上有元素则进入if判断
//否则返回null,即没有找到元素
if ((tab = this.table) != null && (n = tab.length) > 0 && (first = tab[n - 1 & hash]) != null) {
Object k;
//first元素存在,切first元素即锁需要查找的元素,直接返回first.
if (first.hash == hash &&
HashMap源码解读(一) getNode方法
最新推荐文章于 2023-08-24 19:47:17 发布