stream.collect(Collectors.toMap())方法抛出空指针异常
作者:互联网
原因:
Collectors.toMap(),把一个list,转成map,以list元素的 id为key, name 为 value,其中有元素的 value为空,报了空指针,原因如下:
解决方案:
添加过滤,把value为空的元素去掉,加一个 filter
if (null != departments && departments.size() > 0) {
Map<String, String> deptMap = departments.stream().filter(e -> e != null && StringUtil.notEmpty(e.getNo()))
.collect(Collectors.toMap(Department::getUuid, Department::getName, (k1, k2) -> k2));
标签:toMap,stream,Collectors,departments,value,Department 来源: https://www.cnblogs.com/aaacarrot/p/16653244.html