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);
相关推荐
小阳拱白菜25 分钟前
java异常学习
java
程序员岳焱1 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
FrankYoou1 小时前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker
麦兜*2 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了2 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
大只鹅2 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
ai小鬼头2 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
天河归来2 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
weixin_478689763 小时前
十大排序算法汇总
java·算法·排序算法
码荼3 小时前
学习开发之hashmap
java·python·学习·哈希算法·个人开发·小白学开发·不花钱不花时间crud