I have this kind of map Map<String, String> , As you can see there is a inner map “token” and inside this map, there is inner map “tokeninfo”:
{
"id": "44f79d20f28a2ea505b1",
"type": "ONE",
"cardIssued": "AA",
"token": {
"token1": "dummy",
"tokenInfo": {
"aaa": "007"
},
"payment": {
"name": "test",
"paymentType": {
"card": "ONE"
},
"paymentAcc": "123"
},
"isDelegated": false,
"attemptId": "981631"
}
In addition, I have this method :
private static HashMap<String, Object> getReplace(Map<String, Object> message) {
HashMap<String, Object> message = new HashMap<>(message);
for (String mapKey : PARAMETERS_FOR_REPLACE) {
message.computeIfPresent(mapKey,
(key, value) -> REGEX_ANY_CHAR.matcher(String.valueOf(value)).replaceAll("$^%%$"));
}
return message;
}
I need to replace the value of some keys, from the map and inner map. like :
cardIssued ( from the map)
token1 ( from the inner map of the token)
aaa from the inner map of token -> tokeninfo
the value can be in any inner map, not just on the token and tokeninfo
Is there a way to stream all the values to check if it is a map and then call the method and replace it?
final result should be :
{
"id": "44f79d20f28a2ea505b1",
"type": "ONE",
"cardIssued": "$^%%$",
"token": {
"token1": "$^%%$",
"tokenInfo": {
"aaa": "$^%%$"
},
"payment": {
"name": "test",
"paymentType": {
"card": "ONE"
},
"paymentAcc": "123"
},
"isDelegated": false,
"attemptId": "981631"
}
if I run this:
Map<String, Object> newMessage = getReplace(messageMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> (Object) entry.getValue())));
The value will change only in the “top” map.
Is there a way to use stream and do it in all inner and inner “inner” maps?