MutableReactiveMap

A mutable map that also exposes a StateFlow via asStateFlow, allowing for reactive observation of its contents.

It delegates MutableMap functionality to an internal map and emits an immutable Map snapshot to its collectors whenever the map is modified. This enables reactive programming patterns where observers can automatically respond to changes in the map's contents.

Example:

val userMap = reactiveMapOf("name" to "Alice", "age" to "25")

// Observe all changes to the map
userMap.asStateFlow().collect { map ->
println("User data updated: $map")
} // Immediately emits: "User data updated: {name=Alice, age=25}"

// Observe specific key changes
userMap.valueAsFlow("name").collect { name ->
println("Name changed to: $name")
} // Immediately emits: "Name changed to: Alice"

// Modify the map - triggers reactive updates
userMap["city"] = "NYC" // Triggers both flows above
userMap.remove("age") // Triggers map flow, name flow unaffected

Parameters

K

The type of keys in the map.

V

The type of values in the map.

Properties

Link copied to clipboard
abstract override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
Link copied to clipboard
abstract override val keys: MutableSet<K>
Link copied to clipboard
abstract val size: Int
Link copied to clipboard
abstract override val values: MutableCollection<V>

Functions

Link copied to clipboard
abstract fun asStateFlow(): StateFlow<Map<K, V>>

Returns the collection's content as a StateFlow of an immutable collection IC type.

Link copied to clipboard
abstract fun batchNotify(block: MutableMap<K, V>.() -> Unit)

Executes a block of modifications on the underlying mutable collection and notifies observers only once after the block has completed.

Link copied to clipboard
abstract suspend fun batchNotifyAsync(block: suspend MutableMap<K, V>.() -> Unit)

Executes a suspending block of modifications on the underlying mutable collection and notifies observers only once after the block has completed.

Link copied to clipboard
abstract fun clear()
Link copied to clipboard
abstract fun containsKey(key: K): Boolean
Link copied to clipboard
abstract fun containsValue(value: V): Boolean
Link copied to clipboard
abstract operator fun get(key: K): V?
Link copied to clipboard
abstract fun isEmpty(): Boolean
Link copied to clipboard
abstract fun put(key: K, value: V): V?
Link copied to clipboard
abstract fun putAll(from: Map<out K, V>)
Link copied to clipboard
abstract fun remove(key: K): V?
Link copied to clipboard

Returns a new MutableReactiveMap filled with all elements of this collection.

Link copied to clipboard
fun <K, V> MutableReactiveMap<K, V>.valueAsFlow(key: K): Flow<V?>

Creates a Flow that emits the value associated with the specified key whenever this map changes.