返回的值是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值的更多相关文章

  1. 判断Map集合中是否存在某一个key

    方法一: Map<String,String> hashmp = ne HashMap(); hashmp.put("aa", "111"); ha ...

  2. 在java的Map集合中,怎样更改value的值

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/chenyao1994/article/de ...

  3. java 判断Map集合中包含指定的键名,则返回true,否则返回false。

    public static void main(String[] args) { Map map = new HashMap(); //定义Map对象 map.put("apple" ...

  4. Map集合中,关于取值和遍历的相关操作

    这是自己的关于map集合的相关操作的小研究,分享给大家. 主要代码内容包含以下: 1,map集合的遍历 2,根据key值获取value值 3,根据value值获取key值 4,返回最大value值对应 ...

  5. 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集合中,遍历map集合

    package cn.it.zuoye5; import java.io.File;import java.util.HashMap;import java.util.Iterator;import ...

  6. 根据key删除Map集合中的key-value映射

    一:在遍历Map时是不可以删除key-value映射的,如果根据key删除,如下: public static void main(String[] args) { Map<String,Obj ...

  7. Map集合中key不存在时使用toString()方法、valueOf()方法和强制转换((String))之间的区别

    1.toString()方法 底层代码 public String toString() { return this; } 其返回值为String类型的字符串本身 Map<String, Obj ...

  8. 过滤掉map集合中key或value为空的值

    package cn.com.utils; import org.apache.commons.lang3.StringUtils; import java.util.Collection; impo ...

  9. Map集合中的同一键值key重复赋值

    前言: 验证:对Map集合中的同一键值key重复赋值? 结果:对Map集合中的同一键值key重复赋值会覆盖之前的结果. 验证如下: Map<String, Object> map = ne ...

随机推荐

  1. 利用PHP应用程序中的远程文件包含(RFI)并绕过远程URL包含限制

    来源:http://www.mannulinux.org/2019/05/exploiting-rfi-in-php-bypass-remote-url-inclusion-restriction.h ...

  2. 为什么每次登录要手动 source /etc/profile ...

    由于执行顺序如下,故追个查看以下文件,看看是不是 JAVA_HOME, PATH 等环境变量在后面的文件中被重写覆盖了. 1. /etc/profile2. /etc/environment3. ~/ ...

  3. Vue 中 css scoped 样式穿透 ( stylus[>>>] / sass / less[/deep/] )

    scoped看起来很好用,当时在Vue项目中,当我们引入第三方组件库时(如使用element-ui),需要在局部组件中修改第三方组件库样式,而又不想去除scoped属性造成组件之间的样式覆盖.这时我们 ...

  4. 最新 阿里java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.阿里等10家互联网公司的校招Offer,因为某些自身原因最终选择了阿里.6.7月主要是做系统复习.项目复盘.LeetCode ...

  5. nginx 安全配置和限制访问

    nginx基本安全配置 配置ssl证书 listen 443 ssl; ssl_certificate /usr/local/nginx/conf/server.crt; ssl_certificat ...

  6. python+unittest框架第一天unittest之简单认识Test Fixure:测试固件【8月17更新】

    20万的慢慢会实现的吧,hhh unittest框架,我就不在介绍了,百度有很详细的介绍. 我们只要了解: 1.unittest是单元测试框架 2.它提供用例组织与执行:在实际工作中案例可能有上百条, ...

  7. NPM的安装和使用权限问题

    npm之前在默认情况下装过@angular/cli, 安装和使用都没有任何问题, 但是有的包全局安装的时候会提示权限不足, 于是网上搜索了修复此问题的方法, 就是将npm的全局安装目录搬到有权限的文件 ...

  8. [转帖]拿小本本记下的Linux Shell常用技巧(一)

    拿小本本记下的Linux Shell常用技巧(一) https://zhuanlan.zhihu.com/p/73361101 一. 特殊文件: /dev/null和/dev/tty Linux系统提 ...

  9. [DEBUG] Springboot打包jar/war后访问包外的路径

    ======================================================== 我是Ruriko,我爱这个世界:)

  10. oddo

    看了这张图,或许你对odoo有了一些兴趣. https://www.cnblogs.com/wterp/p/9093616.html 这次就是和大家一起交流开源ERP/CRM系统:odoo 对以下读者 ...