JAVA8 Collectors.toMap value为null报错

2018年11月7日 17:59:27 该bug貌似在java9中修复,欢迎补充
2019年3月19日 17:59:11 查看java11的toMap方法后,发现并没有修改任何实现

Caused by: java.lang.NullPointerException
	java.util.HashMap.merge(HashMap.java:1224)
	java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
	java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
	java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
	java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
	java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)

原因是使用Map.merge方法合并时,merge不允许value为null导致的
方法源码:

default V merge(K key, V value,
            BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
        Objects.requireNonNull(remappingFunction);
        //在这里判断了value不可为null
        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;
    }

解决方案:

Map<String,Integer> videoGiftSumVoMap=videoGiftSum.stream().collect(Collectors.toMap
(callRecodVo -> Optional.ofNullable(callRecodVo).map
(CallRecodVo::getStatdate).orElse(0),callRecodVo -> Optional.ofNullable(callRecodVo).map
(CallRecodVo::getVideogiftdiamond).orElse(0), (key1, key2) -> key2));

使用optional判断空指针设置为null的默认值

    原文作者:网瘾少年徐志摩
    原文地址: https://blog.csdn.net/wysnxzm/article/details/81260073
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞