
Implements a key-value data structure where values are deallocated with their keys, useful for caching short-lived objects. Offers operator support and serves as a temporary substitute for a standard library feature.
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.
repositories { mavenCentral() }
implementation("io.github.alongotv:kotlin-multiplatform-weakhashmap:$version")
val intsToStringsMap = weakHashMapOf<Int, String>()
val anyToAnyMap = WeakHashMap<Any, Any>()
val implicitDoubleToStringMap = weakHashMapOf(2.0 to "kotlin")
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.
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.
repositories { mavenCentral() }
implementation("io.github.alongotv:kotlin-multiplatform-weakhashmap:$version")
val intsToStringsMap = weakHashMapOf<Int, String>()
val anyToAnyMap = WeakHashMap<Any, Any>()
val implicitDoubleToStringMap = weakHashMapOf(2.0 to "kotlin")
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.