kotlinweakhashmap

How do I call "put" on a WeakHashMap in Kotlin?


I created a WeakHashMap in Kotlin and for some reason, I am unable to call put it, it won't resolve.

val dataMap: Map<Int, MyData> = WeakHashMap<Int, MyData>()
dataMap.put(myInt, myData) // doesn't resolve

Is there a Kotlin equivalent for a WeakHashMap?


Solution

  • You have cast your WeakHashMap to a read-only Map, so you have restricted it to not have a put function. You should make the variable type MutableMap or leave it as a WeakHashMap.