【问题标题】:Collectors.toMap() giving NPE Internally [closed]Collectors.toMap() 在内部提供 NPE [关闭]
【发布时间】:2020-01-24 13:44:40
【问题描述】:

我正在尝试在流执行中收集数据作为地图。

为了确保我没有任何重复我正在使用 merger function 但最终有 NPE。

见代码sn-p

streamableList().stream()
    .filter(Objects::nonNull)
    .filter(it-> nonNull(it.getKeyHere()))
    .collect(toMap(it -> it.getKeyHere(),
            it -> it.getValueHere(), (a1, a2) -> a1));

见下面的异常

java.lang.NullPointerException: null
        at java.util.HashMap.merge(HashMap.java:1216)
        at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
        at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
        at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        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:499)....
.....and further calls of written code....

谁能提出收集时失败的原因以及如何解决?

【问题讨论】:

  • 什么是streamableList()
  • 你能发一些数据到simulatestreamableList()吗?而且,正如@YCF_L 所询问的,返回的列表类型。
  • it.getValueHere() 是否有可能返回 null?
  • 只要您不添加最小的、可验证的示例,我们就无法提供帮助

标签: java java-8 hashmap java-stream


【解决方案1】:

HashMap::merge 如果任何值为 null,则抛出异常。

@Override
public V merge(K key, V value,
               BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
    if (value == null)
        throw new NullPointerException(); //here

所以getValueHere 必须返回null。您只对项目和键进行空检查,而不是对值进行检查。

您可以添加另一个过滤器,或确保您的类不包含空值。

.filter(it -> Objects.nonNull(it.getValueHere()))

【讨论】:

  • .filter(it-&gt; nonNull(it.getKeyHere()) &amp;&amp; nonNull(it.getValueHere()))
【解决方案2】:

编辑

我刚刚查找了 Java 1.8 中的 HashMap 代码。这是sn-p。当要合并的值作为 null 传递时,它会抛出 NPE。

@Override
public V merge(K key, V value,
               BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
    if (value == null)
        throw new NullPointerException();

原帖

是的,正如@Pshema 指出的那样,当it.getValueHere() 返回null 时,我们会收到此错误。这是复制品。

public class NPECollectorsToMap{

    public static void main( String[] args ){
        Map<String, String> map = doThis();
        System.out.println( map );
    }

    private static Map<String, String> doThis() {
        return streamableList().stream()
        .filter(Objects::nonNull)
        .filter(it-> nonNull(it.getKeyHere()))
        .collect(toMap(it -> it.getKeyHere(),
                it -> it.getValueHere(), (a1, a2) -> a1));
    }

    private static List<It> streamableList(){
        List<It> list = new ArrayList<>();
        list.add( new It( "a", null ) );
        return list;
    }

    private static class It{
        private String keyHere;
        public It( String keyHere, String valueHere ){
            super();
            this.keyHere = keyHere;
            this.valueHere = valueHere;
        }
        private String valueHere;
        public String getKeyHere(){ return keyHere; }
        public void setKeyHere( String keyHere ){ this.keyHere = keyHere; }
        public String getValueHere(){ return valueHere; }
        public void setValueHere( String valueHere ){ this.valueHere = valueHere; }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2011-04-10
    • 2022-01-24
    • 2010-12-20
    • 2018-12-03
    相关资源
    最近更新 更多