zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    微信退款和支付宝退款接口调用(java版)
    SpringBoot实现JWT认证
    param-validate的使用
    SpringBoot统一参数校验开源项目:param-validate
    AOP+自定义注解实现全局参数校验
    Java自定义注解的实现
    Java初级面试题--持续更新
    React-native run-android fail on :app:processDebugResources解决办法
    【原创】Ionic单页应用跳转外链,构造路由返回的解决办法及代码
    angular 负数遇到货币过滤器,负号会变成括号的解决办法
  • 原文地址:https://www.cnblogs.com/youpeng/p/11320409.html
Copyright © 2011-2022 走看看