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);
相关推荐
她说..5 小时前
RabbitMQ 使用场景详解
java·spring boot·后端·spring·rabbitmq·java-rabbitmq
宁渡AI大模型6 小时前
河南宁渡科技有限公司|宁渡课堂 AI 全栈面试分享,AI 应用开发高频考点汇总
java·c++·人工智能·python·深度学习·神经网络·rag
代码什么用6 小时前
JSON一文速通
java·json
Lyyaoo.6 小时前
【二分查找】【中等】搜索二维矩阵/排序数组的第一位和最后位置/搜索旋转排序数组/旋转排序数组中的最小值
java·数据结构·算法
shengjk16 小时前
AI会让程序员失业吗?200年前的手工织工已经给出了答案
后端
土司大王6 小时前
LeetCode hot100——153.寻找旋转排序数组中的最小值:Java 二分模板与 O(log n) 分析
java·算法·leetcode
磐链科技6 小时前
交易所开发中的合规与安全:KYC、AML、冷热钱包设计要点
java·区块链
盖伦发发6 小时前
MySQL InnoDB事务完整教程|ACID、MVCC、隔离级别、事务控制
经验分享·后端·spring·软件工程
一条泥憨鱼6 小时前
苍穹外卖【day09| 对订单的各种操作】
后端·苍穹外卖
码农飞哥6 小时前
企业级RAG系统架构详解
java·人工智能·ai编程·rag·ai应用