Javatpoint Logo
Javatpoint Logo

Kotlin MutableMap Interface

Kotlin MutableMap is an interface of collection framework that holds the object in the form of key and value pair. The values of MutableMap interface are retrieved by using their corresponding keys. The key and value may be of different pairs such as <Int, Int>,<Int, String>, <Char, String> etc. Each key of MutableMap holds only one value.

To use the MutableMap interface we need to use its function called mutableMapOf() or mutableMapOf <k,v>().

Kotlin MutableMap Interface Declaration

Properties of MutableMap

Properties Description
abstract val entries: MutableSet<MutableEntry<K, V>> This returns a MutableSet of all its key and value pairs in the map.
abstract val keys: MutableSet<K> This returns all the keys of MutableSet in this map.
abstract val values: MutableCollection<V> This returns all the values of MutableCollection in the current map. This collection may contain duplicate values.

Function of Kotlin MutableMap

Function Description
abstract fun put(key: K, value: V): V? It adds the given value with the specified key in the map.
abstract fun putAll(from: Map<out K, V>) This updates the current map with key/value pairs from the mentioned map.
abstract fun remove(key: K): V? It removes the specified key with its corresponding value from the map.
open fun remove(key: K, value: V): Boolean It removes the key and value entities from the map only if it exist in the map.
abstract fun clear() This function is used to removes all the elements from the map.
operator fun <K, V> Map<out K, V>.contains(key: K): Boolean It checks the given key in the map.
abstract fun containsKey(key: K): Boolean It returns the true if map contains the specified key.
fun <K> Map<out K, *>.containsKey(key: K): Boolean It returns the true if map contains the specified key.
abstract fun containsValue(value: V): Boolean It returns true if the map maps one or more keys for the given value.
fun <K, V> Map<K, V>.containsValue(value: V): Boolean It returns true if the map maps one or more keys for the given value.
fun <K, V> Map<out K, V>.count(): Int It returns the total number of entities of the map
operator fun <K, V> Map<out K, V>.get(key: K): V? It returns the value corresponding to mention key, or null if no such key found in the map.
fun <K, V> Map<out K, V>.getOrDefault(
key: K,
defaultValue: V
): V
It returns the value with corresponding mention key, or it returns default value if no such mapping for the key in the map.
fun <K, V> Map<K, V>.getOrElse(
key: K,
defaultValue: () -> V
): V
It returns the value for the mention key in the map, or it returns the default value function if no such entry found for the given key.
fun <K, V> Map<K, V>.getValue(key: K): V It returns the value corresponding to given key, or it throws an exception if no key found in the map.

Kotlin MutableMap Example - 1 traversing MutableMap

Let's create an example to create a MutableMap using mutablemapOf() function and traverse it. In this example we create three different types (MutableMap<Int, String>, MutableMap<String, String> and MutableMap<Any, Any>) of MutableMap with different ways.

Output:

.....traverse mutableMap1........
Key = 1, Value = Ashu
Key = 4, Value = Rohan
Key = 2, Value = Ajeet
Key = 3, Value = Vijay
......traverse mutableMap2.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......traverse mutableMap3......
Key = 1, Value = Ashu
Key = name, Value = Rohsan
Key = 2, Value = 200

Kotlin MutableMap Example - 2 put() and putAll()

The function put() and putAll() are used to add the elements in the MutableMap. put() function adds the single element at a time where as putAll() function adds the collection type elements in the MutableMap. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
......traverse mutableMap after mutableMap.putAll(hashMap).......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing

Kotlin MutableMap Example - 3 containsKey()

The containsKey() function is used to check the specified key is present in MutableMap or not. If it contains the specified key, it returns true otherwise it returns false. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......mutableMap.containsKey("city").......
true

Kotlin MutableMap Example - 4 containsValue()

The containsValue() function is used to check the specified value is present in MutableMap or not. This function returns true if the map maps one or more keys for the given value else it returns false. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.......mutableMap.containsValue("Delhi")......
true
.......mutableMap.containsValue("Mumbai")......
false

Kotlin MutableMap Example - 5 contains()

The contains() function is used to check either specified key of value is present in the MutableMap or not. If the specified key or value is present in the MutableMap then it will returns true else it returns false. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......mutableMap.contains("city").......
true

Kotlin MutableMap Example - 6 get(key)

The get(key) function is used to retrieve the corresponding value of specified key in the MutableMap. If no such key is present in the MutableMap it returns null. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.......mutableMap.get("department")......
Development

Kotlin MutableMap Example - 7 getValue(key)

The getValue() function used to returns corresponding value of specified key of the MutableMap or it throws an exception if no key found in map. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.......mutableMap.getValue("department")......
Development

Kotlin MutableMap Example - 8 getOrDefault()

The getOrDefault() function returns corresponding value of specified key of MutableMap. If no such key exists in the MutableMap then it returns default mention value. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.......mutableMap.getOrDefault("name", "Default Value")......
Ashu

Kotlin MutableMap Example - 9 count()

The count() function is used to returns the total number of elements present in the MutableMap. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.....mutableMap.count()........
4

Kotlin MutableMap Example - 10 remove(key) and remove(key, value)

The remove(key) function is used to remove value corresponding to its mention key. Whereas remove(key,value) function removes element containing key and value. The remove(key, value) function returns true if it remove the specified key along with its value else it returns false. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......mutableMap.remove("city").......
Delhi
.......mutableMap.remove("hobby","Playing")......
true
......traverse mutableMap after remove.......
Key = name, Value = Ashu
Key = department, Value = Development

Kotlin MutableMap Example - 11 clear()

The clear() function is used to removes all the elements from the MutableMap. For example:

Output:

......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......mutableMap.clear().......
kotlin.Unit
{}

Next TopicKotlin Set




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA