WeakMap

interface WeakMap<in K, V>(source)

A weak map is a map in which elements can be garbage-collected at any moment.

The exact timing in which elements can be garbage-collected is implementation-defined. Some implementations may allow garbage-collection of keys, some may allow garbage-collection of values, or some entirely different behavior.

This class expresses that an association between two values exists, but we may be capable of regenerating it if it disappears, or we simply don't have an important enough need for it to keep memory.

Unlike regular Map, this interface doesn't allow iteration. It is not possible to observe the contents of this map, since they are ever mutable and elements may disappear at any time. Some implementations may provide such observability features.

Obtain instances

The default implementation is available via the top-level WeakMap function. Other implementations are available in the algorithms subpackage.

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
abstract operator fun contains(key: K): Boolean

Checks whether a value is currently associated with key.

Link copied to clipboard
abstract operator fun get(key: K): V?

Gets the value associated with key in this map.

Link copied to clipboard
inline fun <K, V> WeakMap<K, V>.getOrDefault(key: K, defaultValue: V): V

Attempts to find the value associated with key, returning defaultValue if none is found.

Link copied to clipboard
inline fun <K, V> WeakMap<K, V>.getOrElse(key: K, defaultValue: () -> V): V

Attempts to find the value associated with key, returning the result of defaultValue if none is found.

Link copied to clipboard
inline fun <K, V> WeakMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V

Attempts to find the value associated with key.

Link copied to clipboard
inline operator fun <V> WeakMap<String, V>.getValue(thisRef: Any?, property: KProperty<*>): V?

Allows to use a weak map for data-oriented usage.

Link copied to clipboard
abstract fun remove(key: K): V?

Removes key from this map and returns the previously-stored value.

Link copied to clipboard
fun <K, V> WeakMap<K, V>.remove(key: K, value: V)

Removes the association of key to value from this map.

Link copied to clipboard

Removes the specified keys from this map.

fun <K, V> WeakMap<K, V>.removeAll(from: Map<out K, V>)

Removes the specified associations from this map.

Link copied to clipboard
abstract operator fun set(key: K, value: V)

Associates value to the key.

Link copied to clipboard
fun <K, V> WeakMap<K, in V>.setAll(from: Map<out K, V>)

Sets all the values from the provided map into the current one.

Link copied to clipboard
inline operator fun <V> WeakMap<String, V>.setValue(thisRef: Any?, property: KProperty<*>, value: V)

Allows to use a weak map for data-oriented usage.