Collectors.toMap中的NullPointerException

错误日志如下:

bash 复制代码
java.lang.NullPointerException: null
	at java.util.HashMap.merge(HashMap.java:1226)
	at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
	at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
	at java.util.Collections$2.tryAdvance(Collections.java:4719)
	at java.util.Collections$2.forEachRemaining(Collections.java:4727)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
	at com.xxx.xxx.service.impl.DynamicFormColumnServiceImpl.lambda$getCodePropertyValue$24(DynamicFormColumnServiceImpl.java:465)
	at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1321)
	at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
	at java.util.HashMap$EntrySpliterator.forEachRemaining(HashMap.java:1723)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
	at com.xxx.xxx.service.impl.DynamicFormColumnServiceImpl.getCodePropertyValue(DynamicFormColumnServiceImpl.java:463)

代码:

java 复制代码
Map<String,List<PriceOverseasPostPerson>> listMap=list.stream()
.collect(Collectors.toMap(PriceOverseasPostConfig::getCountryCode,PriceOverseasPostConfig::getCountryManagers));

查看Collectors.toMap方法源码发现,在Collectors.toMap的调用过程中并不是我们平常常用的put方法,而是merge。如下:

java 复制代码
default V merge(K key, V value,
            BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
        Objects.requireNonNull(remappingFunction);
        Objects.requireNonNull(value);
        V oldValue = get(key);
        V newValue = (oldValue == null) ? value :
                   remappingFunction.apply(oldValue, value);
        if(newValue == null) {
            remove(key);
        } else {
            put(key, newValue);
        }
        return newValue;
    }

Objects.requireNonNull(value); value 为null则报空指针错误。

解决方案:

使用filter过滤所有NULL值

java 复制代码
Map<String,List<PriceOverseasPostPerson>> listMap=list.stream().filter(f->CollectionUtil.isNotEmpty(f.getCountryManagers()))
                .collect(Collectors.toMap(PriceOverseasPostConfig::getCountryCode,PriceOverseasPostConfig::getCountryManagers));

java 复制代码
Map<String,List<PriceOverseasPostPerson>> collect = list.stream()
                .collect(HashMap::new, (m, v)->m.put(v.getCountryCode(), v.getCountryManagers()), HashMap::putAll);
相关推荐
用户8356290780512 分钟前
Python 设置 Excel 条件格式教程
后端·python·excel
XuCoder6 分钟前
告别COS,用 GitHub + jsDelivr 搭建零成本图床
后端
武子康11 分钟前
大数据-251 离线数仓 - Airflow 安装部署避坑指南:1.10.11 与 2.x 命令差异、MySQL 配置与错误排查
大数据·后端·apache hive
Memory_荒年19 分钟前
自定义 Spring Boot Starter:手搓“轮子”,但要搓出兰博基尼!
java·后端
bugcome_com22 分钟前
ASP 与ASP.NET核心解析:从经典 ASP 到ASP.NET的演进与实战
后端·asp.net
栈外31 分钟前
我是IDEA重度用户,试了4款AI编程插件:有一款有并发Bug,有一款越用越香
java·后端
小陈同学呦37 分钟前
关于如何使用CI/CD做自动化部署
前端·后端
架构师沉默40 分钟前
为什么说 Go 做游戏服务器就有人皱眉?
java·后端·架构
echome88842 分钟前
Go 语言并发编程实战:用 Goroutine 和 Channel 构建高性能任务调度器
开发语言·后端·golang
a56299161942 分钟前
【springboot】Spring 官方抛弃了 Java 8!新idea如何创建java8项目
java·spring boot·spring