TomJava
TomJava

Reputation: 529

Update a value inside a map in list using java 8

I have the following json object. How to update factor variable if null using java 8 streams?

[{
    "name": "sasi",
    "surname": "test",
    "factor": 3
},
{
    "name": "sasi",
    "surname": "test",
    "factor": null
}]
rawResponseMapList.stream().forEach(map ->map.get("factor")?null:map.put("factor", "TBD")); 

Upvotes: 0

Views: 725

Answers (1)

Flown
Flown

Reputation: 11740

There is a convenience function Map#computeIfAbsent to do just that:

rawResponseMapList.forEach(map -> map.computeIfAbsent("factor", s -> "TBD"));

There is also no need to create Stream if you only want to replace the "factor".

Upvotes: 2

Related Questions