Industrial Training




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


interface MutableMap : Map (source)  

Properties of MutableMap


Properties Description
abstract val entries: MutableSet> 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 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) 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 Map.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 Map.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.


fun main(args: Array) {  
  
    val mutableMap1: MutableMap = mutableMapOf(1 to "Ashu", 4 to "Rohan", 2 to "Ajeet", 3 to "Vijay")  
  
    val mutableMap2: MutableMap = mutableMapOf()  
    mutableMap2.put("name", "Ashu")  
    mutableMap2.put("city", "Delhi")  
    mutableMap2.put("department", "Development")  
    mutableMap2.put("hobby", "Playing")  
    val mutableMap3: MutableMap = mutableMapOf(1 to "Ashu", "name" to "Rohsan", 2 to 200)  
    println(".....traverse mutableMap1........")  
    for (key in mutableMap1.keys) {  
        println("Key = ${key}, Value = ${mutableMap1[key]}")  
    }  
    println("......traverse mutableMap2.......")  
    for (key in mutableMap2.keys) {  
        println("Key = "+key +", "+"Value = "+mutableMap2[key])  
    }  
    println("......traverse mutableMap3......")  
    for (key in mutableMap3.keys) {  
        println("Key = ${key}, Value = ${mutableMap3[key]}")  
    }  
}  

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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
  
  
    val hashMap: HashMap = hashMapOf()  
    hashMap.put("department", "Development")  
    hashMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
    for (key in mutableMap.keys) {  
        println("Key = "+key +", "+"Value = "+mutableMap[key])  
    }  
    mutableMap.putAll(hashMap)  
    println("......traverse mutableMap after mutableMap.putAll(hashMap).......")  
    for (key in mutableMap.keys) {  
        println("Key = "+key +", "+"Value = "+mutableMap[key])  
    }  
}  


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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
    mutableMap.put("department", "Development")  
    mutableMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
  
   for (key in mutableMap.keys) {  
        println("Key = "+key +", "+"Value = "+mutableMap[key])  
    }  
  
    println("......mutableMap.containsKey(\"city\").......")  
    println(mutableMap.containsKey("city"))  
}  


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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
    mutableMap.put("department", "Development")  
    mutableMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
  
   for (key in mutableMap.keys) {  
        println("Key = "+key +", "+"Value = "+mutableMap[key])  
    }  
  
     println(".......mutableMap.containsValue(\"Delhi\")......")  
     println(mutableMap.containsValue("Delhi"))  
    println(".......mutableMap.containsValue(\"Mumbai\")......")  
    println(mutableMap.containsValue("Mumbai"))  
}  


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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
    mutableMap.put("department", "Development")  
    mutableMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
  
   for (key in mutableMap.keys) {  
        println("Key = "+key +", "+"Value = "+mutableMap[key])  
    }  
  
     println("......mutableMap.contains(\"city\").......")  
     println(mutableMap.contains("city"))  
  
}  

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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
    mutableMap.put("department", "Development")  
    mutableMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
  
   for (key in mutableMap.keys) {  
        println("Key = "+key +", "+"Value = "+mutableMap[key])  
    }  
  
    println(".......mutableMap.get(\"department\")......")  
    println(mutableMap.get("department"))  
  
}  

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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
    mutableMap.put("department", "Development")  
    mutableMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
  
   for (key in mutableMap.keys) {  
         println("Key = ${key}, Value = ${mutableMap[key]}")  
    }  
  
    println(".......mutableMap.getValue(\"department\")......")  
    println(mutableMap.getValue("department"))  
  
}  


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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
    mutableMap.put("department", "Development")  
    mutableMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
  
   for (key in mutableMap.keys) {  
       println("Key = ${key}, Value = ${mutableMap[key]}")  
    }  
  
    println(".......mutableMap.getOrDefault(\"name\", \"Default Value\")......")  
    println(mutableMap.getOrDefault("name", "default value"))  
}  


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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
    mutableMap.put("department", "Development")  
    mutableMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
  
   for (key in mutableMap.keys) {  
       println("Key = ${key}, Value = ${mutableMap[key]}")  
    }  
  
    println(".....mutableMap.count()........")  
    println(mutableMap.count())  
}  


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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
    mutableMap.put("department", "Development")  
    mutableMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
  
   for (key in mutableMap.keys) {  
       println("Key = ${key}, Value = ${mutableMap[key]}")  
    }  
  
    println("......mutableMap.remove(\"city\").......")  
    println(mutableMap.remove("city"))  
  
    println(".......mutableMap.remove(\"hobby\",\"Playing\")......")  
    println(mutableMap.remove("hobby","Playing"))  
  
    println("......traverse mutableMap.......")  
    for (key in mutableMap.keys) {  
        println("Key = ${key}, Value = ${mutableMap[key]}")  
    }  
  
}  

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:


fun main(args: Array) {  
  
    val mutableMap: MutableMap = mutableMapOf()  
    mutableMap.put("name", "Ashu")  
    mutableMap.put("city", "Delhi")  
    mutableMap.put("department", "Development")  
    mutableMap.put("hobby", "Playing")  
  
    println("......traverse mutableMap.......")  
  
   for (key in mutableMap.keys) {  
       println("Key = ${key}, Value = ${mutableMap[key]}")  
    }  
  
    println("......mutableMap.clear().......")  
    println(mutableMap.clear())  
    println(mutableMap)  
}  


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




Hi I am Pluto.