java中Hashmap的get方法使用
作者:會挖番薯的pank 时间:2023-10-29 13:10:05
java中Hashmap的get方法
map中存储的是键值对,也就是说通过set方法进行参数和值的存储,之后通过get“键”的形式进行值的读取。
举例
Map map = new Hashmap();//创建一个map
map.put("key","value");//给map赋值
String vlaues = map.get("key");//获取map中键值为“key”的值
system.out.println(vlaues );//输出结果
以上代码的运行结果:
value;
HashMap中get方法的原理
1、首先向get()方法中传递一个key
2、在get()方法中调用hash(key)
如果key!=null,返回该key的哈希值hash = key.hashCode()^ (h >>> 16),否则返回hash=0
3、在get()方法中调用getNode(hash,key)方法
获取该key的节点,并返回value
4、getNode()方法中
首先要判断Hashtable是否为空且table长度大于0且该hash值对应的table元素不为空,条件成立则判断该节点的哈希值是否等于hash,依次遍历该链表或红黑树,查找key==node.key?返回查找到的节点的value
// JDK源码
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
//判断hashtable是否为空,key对应的tab[ ]是否为空
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//判断第一个节点的hash,key是否相等
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;
}
来源:https://blog.csdn.net/weixin_42321329/article/details/88639305
标签:java,Hashmap,get
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring与disruptor集成的简单示例
2021-12-16 11:01:41
Java Hibernate使用SessionFactory创建Session案例详解
2022-03-04 06:21:28
Android Studio如何为Activity添加自定义注解信息
2023-01-06 18:28:04
![](https://img.aspxhome.com/file/2023/2/126672_0s.png)
Android自定义ViewGroup实现淘宝商品详情页
2022-10-28 00:13:01
![](https://img.aspxhome.com/file/2023/5/125415_0s.gif)
android利用消息机制获取网络图片
2023-07-24 09:46:57
![](https://img.aspxhome.com/file/2023/2/85962_0s.jpg)
SpringBoot全局配置long转String丢失精度的问题解决
2023-02-19 22:58:49
C# 9.0 新特性之模式匹配简化的实现
2023-11-06 19:42:15
Python代码一键转Jar包及Java调用Python新姿势
2023-02-13 08:16:22
![](https://img.aspxhome.com/file/2023/4/96944_0s.jpg)
SpringBoot整合OpenApi的实践
2023-08-03 11:59:55
![](https://img.aspxhome.com/file/2023/8/70578_0s.jpg)
C#实现SMTP邮件附件发送功能详解
2022-08-14 10:09:38
![](https://img.aspxhome.com/file/2023/9/89829_0s.png)
Android自定义Banner轮播效果
2023-08-05 23:34:06
使用淘宝ip地址库查ip的示例
2023-02-05 19:19:11
C#表达式中的动态查询详解【译】
2021-07-29 05:19:42
![](https://img.aspxhome.com/file/2023/3/76423_0s.png)
Android 8.0的缓存大小和缓存清理接口方法
2023-11-09 16:47:06
Java移动文件夹及其所有子文件与子文件夹
2023-08-01 09:53:38
谈谈HttpClient使用详解
2022-11-03 08:19:02
IntelliJ IDEA中properties文件显示乱码问题的解决办法
2021-09-08 04:59:03
![](https://img.aspxhome.com/file/2023/4/69884_0s.png)
WebView设置WebViewClient的方法
2023-09-01 05:34:58
Java泛型与数据库应用实例详解
2023-08-14 09:37:15
![](https://img.aspxhome.com/file/2023/4/64014_0s.png)
SpringBoot ApplicationContextAware拓展接口使用详解
2022-04-01 23:20:24
![](https://img.aspxhome.com/file/2023/8/126348_0s.png)