hi all, I've the following nested maps: `Map<St...
# getting-started
a
hi all, I've the following nested maps:
Map<String, Map<String, Map<String, Int>>>
and I'm trying to access it by nesting
if
statements. I am trying to increment the value (
Int
) to one provided as argument to a function. In doing so, I'm encountering an issue:
No set method providing array access
when trying to do something like:
Copy code
val keyMap = cls.parameter!!["key"] as Map<*, *>
// logic here to increment the value of the last key in the nested map
keyMap[nestedKey1][key] = new_value
the last statement is the one failing with the error shown above my questions are: 1. how can I access nested maps in a safe manner and when I detect a key not existing, create it and nest in that if statement creating the rest of the map? 2. how can I set the map on an object (
cls.parameter
) such that I preserve other existing mappings and do not override with the new (and what would be the only if written) mapping?
1
👍 1
Thanks. I've modified all
Map<
to `MutableMap<`and now I'm encountering the issue where I'm trying to access the key in order to set its value. E.g.
map[key1][key2][key3] = value
. In fact what I'm trying to do is Python equivalent of
map[key1][key2][key3] += value
. How can I achieve this value update for the innermost map and set it on a object while preserving the rest of the keys in the nested map?
Right! What I'm trying is
map!![key1]?.get(key2)
and I get an error on key2. Even though it's defined as a nested
MutableMap
.
Nevermind. Just implemented the way you coded it and it works. I have a question: what if
getValue(key2)
returns null? will the lambda create it? or do I have to deal with this case before calling the setting of the value?
I see. How to create non-existing keys in the nested mapping for each level?
Thanks! Will report back should I encounter any issues.
k
Looks like you are fighting against type system. What domain are you trying to model? Is it some config in json/xml? You might want to create your generic class which can hold values of different types, provides type safe access/creation/defaults and supports nesting