Map集合中get不存在的key值
返回的值是null
测试代码
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
String test = map.get("hello");
System.out.println(test);
}
}
运行结果为:
null
从结果可以看出,HashMap集合中,获取不存在的key时并不会报异常.
在Map的实现类HashMap中有这样一段代码
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
*
* <p>A return value of {@code null} does not <i>necessarily</i>
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @see #put(Object, Object)
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
/**
* Implements Map.get and related methods.
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
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;
}
在get方法中并没有向上抛出异常,注释也说明了返回节点或者null
Map集合中get不存在的key值的更多相关文章
- 判断Map集合中是否存在某一个key
方法一: Map<String,String> hashmp = ne HashMap(); hashmp.put("aa", "111"); ha ...
- 在java的Map集合中,怎样更改value的值
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/chenyao1994/article/de ...
- java 判断Map集合中包含指定的键名,则返回true,否则返回false。
public static void main(String[] args) { Map map = new HashMap(); //定义Map对象 map.put("apple" ...
- Map集合中,关于取值和遍历的相关操作
这是自己的关于map集合的相关操作的小研究,分享给大家. 主要代码内容包含以下: 1,map集合的遍历 2,根据key值获取value值 3,根据value值获取key值 4,返回最大value值对应 ...
- 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集合中,遍历map集合
package cn.it.zuoye5; import java.io.File;import java.util.HashMap;import java.util.Iterator;import ...
- 根据key删除Map集合中的key-value映射
一:在遍历Map时是不可以删除key-value映射的,如果根据key删除,如下: public static void main(String[] args) { Map<String,Obj ...
- Map集合中key不存在时使用toString()方法、valueOf()方法和强制转换((String))之间的区别
1.toString()方法 底层代码 public String toString() { return this; } 其返回值为String类型的字符串本身 Map<String, Obj ...
- 过滤掉map集合中key或value为空的值
package cn.com.utils; import org.apache.commons.lang3.StringUtils; import java.util.Collection; impo ...
- Map集合中的同一键值key重复赋值
前言: 验证:对Map集合中的同一键值key重复赋值? 结果:对Map集合中的同一键值key重复赋值会覆盖之前的结果. 验证如下: Map<String, Object> map = ne ...
随机推荐
- 【opencv】split
opencv-split Divides a multi-channel array into several single-channel arrays. code ,,,,,,,,,,,}; Ma ...
- nfs搭建;nfs监控;mount对于nfs的相应配置
nfs搭建 https://www.cnblogs.com/lms0755/p/9211585.html https://www.jianshu.com/p/e47cccbb3ae5 https:// ...
- Git操作记录、腾讯工蜂
腾讯工蜂: http://git.code.tencent.com 登录-->>创建项目-->>获取仓库路径 https://git.code.tencent.com/dang ...
- Struts2中action接收中文参数为乱码解决方法
老实说,中文乱码问题是每个程序员会经常遇到的问题,而且也是一个很头疼的问题.网上很多关于解决中文乱码的帖子,看几个之后你会发现大都是一样的.但是我们照着做,却还是无法解决乱码问题.我也是看了好多帖子, ...
- Introduction - Unsupervised Learning
摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第一章<绪论:初识机器学习>中第4课时<无监督学习>的视频原文字幕.为本人在视频学习过程中逐字逐句 ...
- Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
mysql使用可视化界面登录使用的时候都没问题,只要使用xhell命令进入mysql就报异常 Can't connect to local MySQL server through socket '/ ...
- Java面试 - 在Java中, 既然构造方法是一个方法,那么为什么不使用void 定义呢?
Java程序编译器是根据代码结构来进行编译处理的,执行的时候也是根据代码结构来处理的. 如果在构造方法上使用void,那么此结构就会与普通方法的结构相同,这样编译器会认为此方法是一个 普通方法,而普通 ...
- EMR的fair-scheduler.xml
cat /etc/ecm/hadoop-conf/fair-scheduler.xml <?xml version="1.0"?> <allocations> ...
- 提车应该检查哪?4S店都怕你检查这4个“雷区”,别等后悔才知道
https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9381645601643691163% ...
- spring cloud微服务实践四
spring cloud的hystrix还有一个配搭的库hystrix-dashboard,它是hystrix的一款监控工具,能直观的显示hystrix响应信息,请求成功率等.但是hystrix-da ...