Kotlin MutableMap InterfaceKotlin 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 DeclarationProperties of MutableMap
Function of Kotlin MutableMap
Kotlin MutableMap Example - 1 traversing MutableMapLet'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 |