Duplicate key
说明toMap的时候有重复的key,程序也不知道该取哪个了
Exception in thread "main" java.lang.IllegalStateException: Duplicate key User(id=1, name=null, role=null)
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1255)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)
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.seed.server.utils.SftpUtilTest.main(SftpUtilTest.java:44)
解决办法,根据业务需要,如果不允许出现这样的数据,那就直接抛出异常。如果对于key重复的,根据实际业务人为决定取拿一个,一般要么去第一个要么去最后一个。
NullPointerException
说明toMap的时候有的value是null
Exception in thread "main" java.lang.NullPointerException
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.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)
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.seed.server.utils.SftpUtilTest.main(SftpUtilTest.java:44)
解决办法,根据业务需要,如果不允许出现这样的数据,那就直接抛出异常。如果对于value为null的不进行处理,那就先filter再toMap
连阿里规约都说了