# Map-specific operations In [maps](collection-types.html#map), types of both keys and values are user-defined. Key-based access to map entries enables various map-specific processing capabilities from getting a value by key to separate filtering of keys and values. On this page, we provide descriptions of the map processing functions from the standard library. ## Retrieve keys and values For retrieving a value from a map, you must provide its key as an argument of the [get()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/get.html) function. The shorthand `[key]` syntax is also supported. If the given key is not found, it returns `null`. There is also the function [getValue()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-value.html) which has slightly different behavior: it throws an exception if the key is not found in the map. Additionally, you have more options to handle the key absence: * [getOrElse()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-else.html) works the same way as for lists: the values for non-existent keys are returned from the given lambda function. * [getOrDefault()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-default.html) returns the specified default value if the key is not found. For maps with nullable values, use the following functions instead, which handle missing keys and `null` values explicitly: * `getOrElseIfNull()` returns the result of the specified default value if the key is missing or has a `null` value. * `getOrElseIfMissing()` returns the result of the specified default value if the key is missing. Here's an example that shows the difference between these functions: ```KOTLIN @OptIn(ExperimentalStdlibApi::class) fun main() { //sampleStart val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3) println(numbersMap.get("one")) // 1 println(numbersMap["one"]) // 1 println(numbersMap.getOrDefault("four", 10)) // 10 println(numbersMap["five"]) // null val nullableMap = mapOf("one" to 1, "two" to null) println(nullableMap.getOrElseIfNull("two") { 0 }) // 0 println(nullableMap.getOrElseIfMissing("two") { 0 }) // null // Throws an exception because "six" is missing from the map // numbersMap.getValue("six") //sampleEnd } ``` To perform operations on all keys or all values of a map, you can retrieve them from the properties `keys` and `values` accordingly. `keys` is a set of all map keys and `values` is a collection of all map values. ```KOTLIN fun main() { //sampleStart val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3) println(numbersMap.keys) println(numbersMap.values) //sampleEnd } ``` ## Filter You can [filter](collection-filtering.html) maps with the [filter()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter.html) function as well as other collections. When calling `filter()` on a map, pass to it a predicate with a `Pair` as an argument. This enables you to use both the key and the value in the filtering predicate. ```KOTLIN fun main() { //sampleStart val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11) val filteredMap = numbersMap.filter { (key, value) -> key.endsWith("1") && value > 10} println(filteredMap) //sampleEnd } ``` There are also two specific ways for filtering maps: by keys and by values. For each way, there is a function: [filterKeys()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-keys.html) and [filterValues()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-values.html). Both return a new map of entries which match the given predicate. The predicate for `filterKeys()` checks only the element keys, the one for `filterValues()` checks only values. ```KOTLIN fun main() { //sampleStart val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11) val filteredKeysMap = numbersMap.filterKeys { it.endsWith("1") } val filteredValuesMap = numbersMap.filterValues { it < 10 } println(filteredKeysMap) println(filteredValuesMap) //sampleEnd } ``` ## Plus and minus operators Due to the key access to elements, [plus](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/plus.html) (`+`) and [minus](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/minus.html) (`-`) operators work for maps differently than for other collections. `plus` returns a `Map` that contains elements of its both operands: a `Map` on the left and a `Pair` or another `Map` on the right. When the right-hand side operand contains entries with keys present in the left-hand side `Map`, the result map contains the entries from the right side. ```KOTLIN fun main() { //sampleStart val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3) println(numbersMap + Pair("four", 4)) println(numbersMap + Pair("one", 10)) println(numbersMap + mapOf("five" to 5, "one" to 11)) //sampleEnd } ``` `minus` creates a `Map` from entries of a `Map` on the left except those with keys from the right-hand side operand. So, the right-hand side operand can be either a single key or a collection of keys: list, set, and so on. ```KOTLIN fun main() { //sampleStart val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3) println(numbersMap - "one") println(numbersMap - listOf("two", "four")) //sampleEnd } ``` For details on using [plusAssign](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/plus-assign.html) (`+=`) and [minusAssign](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/minus-assign.html) (`-=`) operators on mutable maps, see [Map write operations](#map-write-operations) below. ## Map write operations [Mutable](collections-overview.html#read-only-and-mutable-collections) maps offer map-specific write operations. These operations let you change the map content using the key-based access to the values. There are certain rules that define write operations on maps: * Values can be updated. In turn, keys never change: once you add an entry, its key is constant. * For each key, there is always a single value associated with it. You can add and remove whole entries. Below are descriptions of the standard library functions for write operations available on mutable maps. ### Add and update entries To add a new key-value pair to a mutable map, use [put()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/put.html). When a new entry is put into a `LinkedHashMap` (the default map implementation), it is added so that it comes last when iterating the map. In sorted maps, the positions of new elements are defined by the order of their keys. ```KOTLIN fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2) numbersMap.put("three", 3) println(numbersMap) //sampleEnd } ``` To add multiple entries at a time, use [putAll()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/put-all.html). Its argument can be a `Map` or a group of `Pair`s: `Iterable`, `Sequence`, or `Array`. ```KOTLIN fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3) numbersMap.putAll(setOf("four" to 4, "five" to 5)) println(numbersMap) //sampleEnd } ``` Both `put()` and `putAll()` overwrite the values if the given keys already exist in the map. Thus, you can use them to update the values of map entries. ```KOTLIN fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2) val previousValue = numbersMap.put("one", 11) println("value associated with 'one', before: $previousValue, after: ${numbersMap["one"]}") println(numbersMap) //sampleEnd } ``` You can also add new entries to maps using the shorthand operator form. There are two ways: * [plusAssign](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/plus-assign.html) (`+=`) operator. * the `[]` operator alias for `set()`. ```KOTLIN fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2) numbersMap["three"] = 3 // calls numbersMap.put("three", 3) numbersMap += mapOf("four" to 4, "five" to 5) println(numbersMap) //sampleEnd } ``` When called with the key present in the map, operators overwrite the values of the corresponding entries. #### Add default values for missing entries To return an existing value or add a default value when no value is available, use the [.getOrPut()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-put.html) extension function. If the key is missing or has a `null` value, `.getOrPut()` stores the default value and returns it. For maps with nullable values, you can use the `.getOrPutIfNull()` and `.getOrPutIfMissing()` functions to control how `null` values are treated: * `getOrPutIfNull()` behaves like `getOrPut()` and uses the default value if the key is missing or has a `null` value. * `getOrPutIfMissing()` uses the default value only if the key is missing. The `getOrPutIfNull()` and `getOrPutIfMissing()` functions are [Experimental](components-stability.html#stability-levels-explained). To opt in, use the `@OptIn(ExperimentalStdlibApi::class)` annotation. Here's an example: ```KOTLIN @OptIn(ExperimentalStdlibApi::class) fun main() { //sampleStart val mapForNull = mutableMapOf("one" to null) val mapForMissing = mutableMapOf("one" to null) // Replaces the value because "one" has a null value mapForNull.getOrPutIfNull("one") { 1 } println(mapForNull) // {one=1} // Keeps the null value because "one" exists in the map mapForMissing.getOrPutIfMissing("one") { 1 } println(mapForMissing) // {one=null} //sampleEnd } ``` ### Remove entries To remove an entry from a mutable map, use the [remove()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/remove.html) function. When calling `remove()`, you can pass either a key or a whole key-value-pair. If you specify both the key and value, the element with this key will be removed only if its value matches the second argument. ```KOTLIN fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3) numbersMap.remove("one") println(numbersMap) numbersMap.remove("three", 4) //doesn't remove anything println(numbersMap) //sampleEnd } ``` You can also remove entries from a mutable map by their keys or values. To do this, call `remove()` on the map's keys or values providing the key or the value of an entry. When called on values, `remove()` removes only the first entry with the given value. ```KOTLIN fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3, "threeAgain" to 3) numbersMap.keys.remove("one") println(numbersMap) numbersMap.values.remove(3) println(numbersMap) //sampleEnd } ``` The [minusAssign](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/minus-assign.html) (`-=`) operator is also available for mutable maps. ```KOTLIN fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3) numbersMap -= "two" println(numbersMap) numbersMap -= "five" //doesn't remove anything println(numbersMap) //sampleEnd } ```