Created
December 13, 2019 05:34
-
-
Save upangka/610f44ffdb9bd25bb7fcba148ee07514 to your computer and use it in GitHub Desktop.
Collectors.toMap null问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MapRecordTest { | |
public static void main(String[] args) { | |
Map<String,Object> myMap = new HashMap<>(); | |
myMap.put("name", null); | |
System.out.println(myMap.entrySet()); | |
Map<String, Object> newMap = myMap.entrySet().stream().collect(Collectors.toMap(entry -> { | |
System.out.println(entry); | |
return entry.getKey().toUpperCase(); | |
}, entry -> { | |
System.out.println(entry); | |
return entry.getValue(); | |
})); | |
System.out.println(newMap); | |
} | |
} | |
/** | |
[name=null] | |
name=null | |
name=null | |
Exception in thread "main" java.lang.NullPointerException | |
at java.util.HashMap.merge(Unknown Source) | |
at java.util.stream.Collectors.lambda$toMap$58(Unknown Source) | |
at java.util.stream.ReduceOps$3ReducingSink.accept(Unknown Source) | |
at java.util.HashMap$EntrySpliterator.forEachRemaining(Unknown Source) | |
at java.util.stream.AbstractPipeline.copyInto(Unknown Source) | |
at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source) | |
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source) | |
at java.util.stream.AbstractPipeline.evaluate(Unknown Source) | |
at java.util.stream.ReferencePipeline.collect(Unknown Source) | |
at com.vpu.mp.controller.admin.member.MapRecordTest.main(MapRecordTest.java:19) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
解决办法 stackflow java-8-nullpointerexception-in-collectors-tomap