mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
处理了CollStreamUtil.toIdentityMap中,如果value为空时,发生NPE异常问题
This commit is contained in:
parent
a2331bf5c9
commit
1b28ba13e4
@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.collection;
|
||||
|
||||
|
||||
import cn.hutool.core.lang.Opt;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.stream.StreamUtil;
|
||||
|
||||
@ -48,7 +49,7 @@ public class CollStreamUtil {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.collect(Collectors.toMap(key, Function.identity(), (l, r) -> l));
|
||||
.collect(HashMap::new, (HashMap<K, V> m, V v) -> m.put(Opt.ofNullable(v).map(key).get(), v), HashMap::putAll);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -28,6 +28,11 @@ public class CollStreamUtilTest {
|
||||
Assert.assertEquals(map.get(2L).getName(), "李四");
|
||||
Assert.assertEquals(map.get(3L).getName(), "王五");
|
||||
Assert.assertNull(map.get(4L));
|
||||
|
||||
// 测试value为空时
|
||||
list.add(null);
|
||||
map = CollStreamUtil.toIdentityMap(list, Student::getStudentId);
|
||||
Assert.assertNull(map.get(4L));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user