HashMap源码解析——get方法
来源:互联网 发布:方舟低配优化补丁 编辑:程序博客网 时间:2024/06/05 19:31
public V get(Object key) { Node<K,V> e; return (e = getNode(hash(key), key)) == null ? null : e.value; }
getNode()方法
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) 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算法作为基础,理解起来这段代码就很容易了。从上面的源代码中可以看出:从HashMap中get元素时,首先计算key的hashCode,找到数组中对应位置的某一元素,然后通过key的equals方法在对应位置的中找到需要的元素。
阅读全文
0 0
- HashMap源码解析——get方法
- java 8 Hashmap深入解析 —— put get 方法源码
- java 8 Hashmap深入解析 —— put get 方法源码
- java 8 Hashmap深入解析 —— put get 方法源码
- java 8 Hashmap深入解析 —— put get 方法源码
- HashMap源码解析——构造方法
- HashMap源码解析——put方法
- HashMap源码--(四)get方法
- HashMap源码解析——概述
- HashMap源码解析——关键属性
- HashMap源码解析——学习笔记
- HashMap中的数据结构与get,put源码解析
- HashMap之put方法源码解析
- HashMap源码注解 之 get()方法(五)
- HashMap.put/get方法
- Android源码解析 -- HashMap
- HashMap源码解析
- HashMap源码解析
- ACM 斯特林公式 Factorial vs Power
- 环境配置-nginx安装
- 【洛谷2258】子矩阵
- 求组合数取模(杨辉三角打表 & 求逆元(扩展欧几里得、费马小定理、欧拉定理、线性求法) & Lucas)
- -oracle获取随机数大写字母
- HashMap源码解析——get方法
- for in 语句
- Oracle 删除重复数据只留一条
- 错误WARN OgnlValueStack:60
- llinux设备驱动并发与竞争简介
- ora-12154:TNS:无法解析指定的连接标识符
- phantomjs对页面的某个div截图
- NYOJ 283 对称排序
- VisuaGDB 编译、调试libev