WeakHashMap is a key-value data structure, where value gets deallocated after the key is deallocated. This data structure could come in handy, if you have short-lived objects to store in a Cache.
Warning
Please be advised that this project makes use of Experimental (WeakReference) and Beta (actual classes) components of Kotlin. More details on stability can be found there.
At the moment the project is targeting Apple platforms and JVM.
- Add Maven Central to the list of your repositories
repositories { mavenCentral() }
- Add WeakHashMap as a dependency where it is required
implementation("io.github.alongotv:kotlin-multiplatform-weakhashmap:$version")
- Declare a variable containing the map. There is a number of ways to do that:
val intsToStringsMap = weakHashMapOf<Int, String>()
val anyToAnyMap = WeakHashMap<Any, Any>()
val implicitDoubleToStringMap = weakHashMapOf(2.0 to "kotlin")
- Call WeakHashMap functions (they also support operator calls) to populate the data structure with data like you would do with stdlib HashMap.
This code block will print "4":
val intsToIntsMap = weakHashMapOf<Int, Int>() intsToIntsMap[2] = 3 intsToIntsMap.set(2, 4) println(intsToIntsMap[2])
This is a substitute to be used until ticket for adding WeakHashMap to Kotlin's stdlib is resolved.
The scheme below shows an approximate architecture of
the WeakHashMap.
When Key Bearer Class is collected by Garbage Collector, the Key Object and Value Object will get removed from WeakHashMap and deallocated too, provided that Key Object has not been retained by some other object.