Map中的Null key, Null Value
程序员文章站
2022-05-09 18:53:23
...
ConcurrentHashMap的key和value都不能为null,否则抛NullPointerException
java.util.concurrent.ConcurrentHashMap.put(K, V)
hashMap和linkedhashMap都允许null key和null value,
treeMap不允许null key,但允许null value
http://www.trinea.cn/android/android-source-code-analysis/concurrenthashmap-nullpointerexception-null-key/
java.util.concurrent.ConcurrentHashMap.put(K, V)
/** * Maps the specified key to the specified value in this table. * Neither the key nor the value can be null. * * <p> The value can be retrieved by calling the <tt>get</tt> method * with a key that is equal to the original key. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt> * @throws NullPointerException if the specified key or value is null */ public V put(K key, V value) { if (value == null) throw new NullPointerException(); int hash = hash(key.hashCode()); return segmentFor(hash).put(key, hash, value, false); }
hashMap和linkedhashMap都允许null key和null value,
treeMap不允许null key,但允许null value
http://www.trinea.cn/android/android-source-code-analysis/concurrenthashmap-nullpointerexception-null-key/
推荐阅读
-
在javascript中,null>=0 为真,null==0却为假,null的值详解
-
【Java必修课】通过Value获取Map中的键值Key的四种方法
-
有关javascript中Null和Undefined的区别讲解
-
js中null和undefined的区别讲解
-
Android获取清单文件中的meta-data,解决碰到数值为null的问题
-
PHP中删除变量时unset()和null的区别分析
-
ASP.NET的ASHX一般处理程序中Session为null且不能赋值
-
php中is_null,empty,isset,unset 的区别详细介绍
-
MySQL数据库中null的知识点总结
-
如何在Asp.Net Core MVC中处理null值的实现