I am performing some transformations using a Java stream on some data. I would like to ignore any values in a map that contain a null value.
Example:
HashMap<String, Object> vals = new HashMap<>();
vals.put("null", null);
vals.put("string1", "1 ");
I've tried using filter:
Map<String, Object> nullMap = vals.entrySet().stream()
.filter(e -> e.getValue() != null)
.collect(Collectors.toMap(
e -> e.getKey().trim(),
e -> (e.getValue() instanceof String ? ((String) e.getValue()).trim() : e.getValue())))
However the returned object has the null key/value pair (in my example "null" -> null) completely removed. How can I retain the key "null" and ignore it during any subsequent processing after the .filter step in the Java stream?
In the above example I would like to get a Map returned with where the key and value pairs are trimmed and the null value is 'ignored' during the trimming step.