KMap

class KMap<K, V>(initialEntries: List<Pair<K, V>> = emptyList())(source)

A reactive key-value map.

Port of the useMap React hook.

val map = KMap<String, Int>()
map.set("apples", 3)
map.set("bananas", 5)

println(map.get("apples")) // 3
println(map.has("bananas")) // true

map.remove("apples")
map.clear()

// Observe reactively:
map.state.collect { snapshot -> render(snapshot) }

Parameters

initialEntries

Key-value pairs to pre-populate the map.

Constructors

Link copied to clipboard
constructor(initialEntries: List<Pair<K, V>> = emptyList())

Properties

Link copied to clipboard

true when the map contains no entries.

Link copied to clipboard
val size: Int

Number of entries currently in the map.

Link copied to clipboard
val snapshot: Map<K, V>

The current map snapshot.

Link copied to clipboard
val state: StateFlow<Map<K, V>>

Immutable snapshot of the current map contents as a StateFlow.

Functions

Link copied to clipboard
fun clear()

Removes all entries.

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

Returns the value for key, or null if absent.

Link copied to clipboard
fun has(key: K): Boolean

Returns true when the map contains key.

Link copied to clipboard
fun remove(key: K)

Removes the entry for key. No-op if absent.

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

Associates key with value, replacing any existing entry.

Link copied to clipboard
open override fun toString(): String