withDefault

fun <K, V> Map<K, V>.withDefault(defaultValue: (key: K) -> V): Map<K, V>(source)

Returns a wrapper of this read-only map, having the implicit default value provided with the specified function defaultValue.

This implicit default value is used when the original map doesn't contain a value for the key specified and a value is obtained with Map.getValue function, for example when properties are delegated to the map.

When this map already has an implicit default value provided with a former call to withDefault, it is being replaced by this call.

Since Kotlin

1.0

Samples

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

fun main() { 
   //sampleStart 
   val mapWithDefault = mapOf(1 to "One", 2 to "Two").withDefault { key ->
    when {
        key == 0 -> "Zero"
        key > 0 -> "Positive"
        else -> "Negative"
    }
}
val actual = (-1..3).associateWith { mapWithDefault.getValue(it) }
println(actual) // {-1=Negative, 0=Zero, 1=One, 2=Two, 3=Positive} 
   //sampleEnd
}
import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val mapWithDefault = mapOf(1 to "One", 2 to "Two").withDefault { _ -> "Other" }
println(mapWithDefault.getValue(0)) // Other
val mapWithReplacedDefault = mapWithDefault.withDefault { _ -> "Unknown" }
println(mapWithReplacedDefault.getValue(0)) // Unknown 
   //sampleEnd
}

@JvmName(name = "withDefaultMutable")
fun <K, V> MutableMap<K, V>.withDefault(defaultValue: (key: K) -> V): MutableMap<K, V>(source)

Returns a wrapper of this mutable map, having the implicit default value provided with the specified function defaultValue.

This implicit default value is used when the original map doesn't contain a value for the key specified and a value is obtained with Map.getValue function, for example when properties are delegated to the map.

When this map already has an implicit default value provided with a former call to withDefault, it is being replaced by this call.

Since Kotlin

1.0

Samples

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

fun main() { 
   //sampleStart 
   val mapWithDefault = mapOf(1 to "One", 2 to "Two").withDefault { key ->
    when {
        key == 0 -> "Zero"
        key > 0 -> "Positive"
        else -> "Negative"
    }
}
val actual = (-1..3).associateWith { mapWithDefault.getValue(it) }
println(actual) // {-1=Negative, 0=Zero, 1=One, 2=Two, 3=Positive} 
   //sampleEnd
}
import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val mapWithDefault = mapOf(1 to "One", 2 to "Two").withDefault { _ -> "Other" }
println(mapWithDefault.getValue(0)) // Other
val mapWithReplacedDefault = mapWithDefault.withDefault { _ -> "Unknown" }
println(mapWithReplacedDefault.getValue(0)) // Unknown 
   //sampleEnd
}
import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val mutableMap = mutableMapOf(1 to "One", 2 to "Two")
val mutableMapWithDefault = mutableMap.withDefault { _ -> "Other" }
println(mutableMapWithDefault.getValue(0)) // Other
mutableMapWithDefault[0] = "Zero"
println(mutableMapWithDefault.getValue(0)) // Zero
println(mutableMap.getValue(0)) // Zero 
   //sampleEnd
}