I have this block of code to create a deep copy of a Map<String, Object>:
Map<String, Object> originalMap = new HashMap<>();
originalMap.put("key1", "value1");
originalMap.put("key2", null);
originalMap.put("key3", "value3");
Map<String, Object> newMap = originalMap.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
// Map.Entry::getValue,
// entry -> entry.getValue() != null ? entry.getValue() : null,
entry -> entry.getValue() != null ? entry.getValue() : "",
(oldValue, newValue) -> oldValue,
HashMap::new
));
System.out.println("Original map: " + originalMap);
System.out.println("New map: " + newMap);
If I use 2 commented line, this codeblock throw Null Pointer Exception
and I don't understand this. I know that HashMap does allow Null values so why it's not accept the value mapper
to return null?