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);
相关推荐
AKAMAI12 分钟前
云计算迁移策略:分步框架与优势
后端·云原生·云计算
ruokkk17 分钟前
eureka如何绕过 LVS 的虚拟 IP(VIP),直接注册服务实例的本机真实 IP
后端
codeRichLife20 分钟前
Mybatisplus3.5.6,用String处理数据库列为JSONB字段
java·数据库
AKAMAI24 分钟前
为何AI推理正推动云计算从集中式向分布式转型
后端·云原生·云计算
来自星星的猫教授26 分钟前
Java 文件注释规范(便于生成项目文档)
java·注释
zhimeng329 分钟前
自己学习原理
java
程序员鱼皮32 分钟前
学 Java 还是 Go 语言?这事儿很简单!
java·后端·计算机·程序员·开发·编程经验·自学编程
Lanqing_076037 分钟前
淘宝商品详情图API接口返回参数说明
java·服务器·前端·api·电商
天蓝的那一角43 分钟前
你想要的Lambda第二弹
后端
矮油0_o1 小时前
第一部分 -- ①语法分析的概要
java·编译器·解释器·语法分析