Map map1 = new HashMap();
map1.put("key1", "value1");
map1.put("key2", "value2");
map1.put("key3", "");
map1.put("key4", null);
System.out.println(map1);
运行结果如下:
{key1=value1, key2=value2, key3=, key4=null}
Map map1 = new ConcurrentHashMap();
map1.put("key1", "value1");
map1.put("key2", "value2");
map1.put("key3", "");
map1.put("key4", null); // 此处抛出异常
System.out.println(map1);
报错如下:
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011)
at java.base/java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006)
at pck1.Test0824.main(Test0824.java:28)
查看 ConcurrentHashMap
的 put()
方法,如下:
/**
* Maps the specified key to the specified value in this table.
* Neither the key nor the value can be null.
*
* The value can be retrieved by calling the {@code get} 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 {@code key}, or
* {@code null} if there was no mapping for {@code key}
* @throws NullPointerException if the specified key or value is null
*/
public V put(K key, V value) {
return putVal(key, value, false);
}
在 key
和 value
上都有 @NotNull
注解,且注释里也提到了,如果 key
或者 value
是 null
,就会抛出 NullPointerException
。
再查看 putVal()
方法,如下:
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
......
HashMap
可以插入null值,但是 ConcurrentHashMap
不能插入null值,否则会抛NPE异常。
Properties p = new Properties();
p.put("key1", "value1");
p.put("key2", "value2");
p.put("key3", "");
// p.put("key4", null);
System.out.println(p);
Object obj = p.getProperty("key3");
System.out.println(obj == null ? "null" : "not null");
运行结果如下:
{key1=value1, key2=value2, key3=}
not null
其中, key3
的值是空字符串( ""
),而不是null。
如果试图插入null值(将上面代码中的 key4
所在行反注释),则报错如下:
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011)
at java.base/java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006)
at java.base/java.util.Properties.put(Properties.java:1301)
at pck1.Test0824.main(Test0824.java:14)
从调用栈可见, Properties
使用的是 ConcurrentHashMap
,所以,显然无法插入null值。
创建文件 test.properties
如下:
key1=value1
key2=value2
key3=
读取 test.properties
文件:
Properties properties = new Properties();
InputStream in = Test0824.class.getClassLoader().getResourceAsStream("pck1/test.properties");
try {
properties.load(in);
String value1 = properties.getProperty("key1");
System.out.println(value1);
String value2 = properties.getProperty("key2");
System.out.println(value2);
String value3 = properties.getProperty("key3");
System.out.println(value3);
System.out.println(value3 == null); // false
System.out.println(value3.equals("")); // true
String value4 = properties.getProperty("key4");
System.out.println(value4);
System.out.println(value4 == null); // true
} finally {
in.close();
}
运行结果如下:
value1
value2
false
true
null
true
可见,对于 key3=
这样的配置,读取的value是空字符串( ""
),而不是null值。而对于不存在的key值(比如本例中的 key4
),读取的value是null值。
反过来想,之所以Properties里除非是不存在的key值,否则读取和插入都不存在null值的情况,也许目的正是为了区分key值是否存在吧。
只有读取不存在的key值时,才会出现null值,否则无论读取或插入,都不存在value为null值的情况(读取为空字符串,插入报错NPE)。
具体如下:
key=
这样的配置,读取的value是空字符串( ""
);