getOrPut

inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V(source)

Returns a non-null value for key, or computes and stores a default value.

For a non-null existing value, defaultValue is not invoked. An absent key or a stored null invokes defaultValue once, then stores its result, including null. A null result is computed again on a later call. If the callback inserts this key itself, its returned default replaces that value.

The callback is inline and supports non-local returns. Exceptions and non-local returns skip the implicit insertion; explicit callback mutations retain their effects. This operation is not atomic.

Receiver

mutable map to read and possibly update.

Return

the existing non-null value, or the computed and stored default, possibly null.

Parameters

key

key to look up or insert.

defaultValue

callback supplying a value for an absent key or an existing null value.

See also