victor-mntl
11/25/2019, 4:44 PMval mutableMap<String, mutableSet<String>> myMap = mutableMapOf ("first key" to mutableSetOf("value1 of first entry","value2 of first entry"))
if (myMap["another key"]==null)
myMap["another key"] = mutableSetOf("value1 of second entry")
else
//at this point, we know that myMap["another key"] is NOT NULL
myMap["another key"].add("value1 of second entry")
It marks the last line (marked in bold) and says that I should use ?. instead of . because it says the receiver of the call is of type MutableSet<String>?
(nullable) but it is verified that it is not. Am I missing something?Mike
11/25/2019, 6:47 PMmyMap.getOrPut("another key", { mutableSetOf() }).add("value 1 of second entry")
victor-mntl
11/25/2019, 7:35 PM.getOrPut()
is a lambda, so it must go between {}
, myMap.getOrPut("another key", *{* mutableSetOf~*<String>*~() *}* ).add("value 1 of second entry")
(Mike was good at his solution in the way that getOrPut()
returns the value of the entry, even if it is the one entry just created because it didn't exist previously).
2019-11-26 EDIT: the <String>
part is not needed, the Kotlin type inference engine (on actual release version) is able to run it. It is more simple this way, only adding the {}
that were missing on the first version wrote by Mike, as it is a lamba function. =)Mike
11/25/2019, 7:52 PMvictor-mntl
11/25/2019, 7:53 PM<String>
is missing. Kotlin didn't seem to infer it.Mike
11/25/2019, 8:57 PMval test = mutableMapOf<String, MutableSet<String>>()
test.getOrPut("value", { mutableSetOf() }).add("valueentry")
Are you using 1.3.41 or earlier?victor-mntl
11/26/2019, 12:15 PM<String>
... Sorry for that :'). Runing 1.3.60. Edited my prior message to avoid confusion for future readers of this thread.Mike
11/26/2019, 12:49 PMvictor-mntl
11/26/2019, 12:51 PM