大家好,我是程序员行一
本文介绍一下我在使用java.util.HashMap
中遇到的一个NPE问题,实际上是因为个人对API了解不深入导致使用时出现意想不到的问题
先来介绍bug现象
// 这段代码编译时没有任何问题的
@Test
public void testPutIfAbsentBug() {
final HashMap<String, Boolean> hashMap = new HashMap<>(2);
String key = "行一";
// 这里想表达的是,如果put成功就开始我的业务逻辑
if(hashMap.putIfAbsent(key,true)){
// 业务逻辑
}
}
执行结果
java.lang.NullPointerException
at com.github.xfc.map.HashMapTest.testPutIfAbsentBug(HashMapTest.java:205)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at
// 忽略部分无关异常
ps:如果您已经看出是什么问题了,本文可以忽略
原因分析
这里跟前面分析的
java.util.HashMap
源码有关,有兴趣可以看下 深入理解HashMap一文
java.util.HashMap#putIfAbsent源码查看
// 通过此方法可以看出,putIfAbsent方法实际上是调用的putVal方法,onlyIfAbsent=true
// 意为:如果该字段为true,如果hashMap中已经存在相同的key,则不再进行覆盖,而是直接返回原来的key对应的value
@Override
public V putIfAbsent(K key, V value) {
return putVal(hash(key), key, value, true, true);
}
//
if (e != null) { // existing mapping for key
V oldValue = e.value;
// 如果为true,这个if不会进入,说明不会替换新的值,直接返回value
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
为什么这里会NPE呢
原因是如果第一次put的时候,原hashMap中无相同的key,会正常走put流程,则if (e != null)不满足,一次会走如下的代码
if (e != null) if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
// 会走这里直接返回空,因此我们的测试代码里面就成了if(null),因此直接抛出NPE
return null;
验证首次调用putIfAbsent前已经有相同的key是否会NPE
@Test
public void testPutIfAbsentBugIfExisted() {
final HashMap<String, Boolean> hashMap = new HashMap<>(2);
String key = "行一";
// 这里put,保证调用putIfAbsent前已经有元素
hashMap.put(key,true);
if(hashMap.putIfAbsent(key,true)){
// 业务逻辑
}
}
// 执行成功
执行结果
总结
在使用hashMap的API时很有必要详细了解其特性,不要盲目使用,否则可能会遇到意想不到的异常错误。
-----------欢迎关注------------