getOrPut

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

Returns the value for the given key if the value is present and not null. Otherwise, calls the defaultValue function, puts its result into the map under the given key, and returns the call result.

Note: it's not recommended to use this function if the map is expected to contain null values. Use either getOrPutIfNull, or getOrPutIfMissing instead to express the intent what to do when the key is mapped to null value more clearly.

When the given key is not in this map or is mapped to a null, the result of defaultValue, even if null, is put into the map under the key. If defaultValue throws an exception, the exception is rethrown.

Note that the operation is not guaranteed to be atomic if the map is being modified concurrently.

Since Kotlin

1.0

Throws

if the specified key or the result of defaultValue is null, and this map does not support null keys or values.

Samples

import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val map = mutableMapOf<String, Int?>()

println(map.getOrPut("x") { 2 }) // 2
// subsequent calls to getOrPut do not evaluate the default value
// since the first getOrPut has already stored value 2 in the map
println(map.getOrPut("x") { 3 }) // 2

// however, null value mapped to a key is treated the same as the missing value
println(map.getOrPut("y") { null }) // null
// so in that case the default value is evaluated
println(map.getOrPut("y") { 42 }) // 42 
   //sampleEnd
}
inline fun <K, V> ConcurrentMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V(source)

Returns the value for the given key if the value is present and not null. Otherwise, calls the defaultValue function, puts its result into the map under the given key, and returns the call result.

Note: it's not recommended to use this function if the map is expected to contain null values. Use either getOrPutIfNull, or getOrPutIfMissing instead to express the intent what to do when the key is mapped to null value more clearly.

When the given key is not in this map or is mapped to a null, the result of defaultValue, even if null, is put into the map under the key. If defaultValue throws an exception, the exception is rethrown.

This method guarantees not to put the value into the map if the key is already there, but the defaultValue function may be invoked even if the key is already in the map.

Since Kotlin

1.0

Samples

import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val map = mutableMapOf<String, Int?>()

println(map.getOrPut("x") { 2 }) // 2
// subsequent calls to getOrPut do not evaluate the default value
// since the first getOrPut has already stored value 2 in the map
println(map.getOrPut("x") { 3 }) // 2

// however, null value mapped to a key is treated the same as the missing value
println(map.getOrPut("y") { null }) // null
// so in that case the default value is evaluated
println(map.getOrPut("y") { 42 }) // 42 
   //sampleEnd
}