Java Collectors.toMap()在内部提供NPE
我试图在流执行中收集数据作为映射 为了确保我没有任何重复,我使用合并功能,但最终有NPE 请参阅代码片段Java Collectors.toMap()在内部提供NPE,java,java-8,hashmap,java-stream,Java,Java 8,Hashmap,Java Stream,我试图在流执行中收集数据作为映射 为了确保我没有任何重复,我使用合并功能,但最终有NPE 请参阅代码片段 streamableList().stream() .filter(Objects::nonNull) .filter(it-> nonNull(it.getKeyHere())) .collect(toMap(it -> it.getKeyHere(), it -> it.getValueHere(), (a1, a2) -
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....
有人能提出收集时失败的原因和解决方法吗?Edit
我只是从Java1.8中查找HashMap代码。下面是片段。当要合并的值作为null传递时,它抛出一个NPE
如果任何值为空,则合并将抛出
@Override
public V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
if (value == null)
throw new NullPointerException(); //here
什么是StreamableList?您可以将一些数据发布到simulatestreamableList吗?另外,正如@YCF_L所问的,返回的列表类型。是否有可能it.getValueHere返回null?只要您不添加最小的、可验证的示例,我们就无法帮助或.filterit->nonNullit.getKeyHere&&nonNullit.getValueHere
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; }
}
}
@Override
public V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
if (value == null)
throw new NullPointerException(); //here
.filter(it -> Objects.nonNull(it.getValueHere()))