Kotlin mutableMapOf()

Last Updated : 15 Jun, 2025

In Kotlin, mutableMapOf() is used to create a MutableMap. A MutableMap holds key-value pairs where keys are unique, but values can be duplicated. This means that each key points to exactly one value, but two or more keys can point to the same value.

For example, we can create maps like:

  • <Int, String> - where the key is an integer and the value is a string.
  • <Char, String> - where the key is a character and the value is a string

Syntax:

1. To create an empty MutableMap:

val map = mutableMapOf<K, V>()

This creates an empty MutableMap.

2. To create a MutableMap with key-value pairs:

val map = mutableMapOf(key1 to value1, key2 to value2)

Example:

Kotlin
fun main() {
    val map = mutableMapOf("Box" to 12, "Books" to 18, "Table" to 13)

    println("Entries: $map")
    println("Keys: ${map.keys}")
    println("Values: ${map.values}")
}

Output: 

Entries: {Box=12, Books=18, Table=13}
Keys: [Box, Books, Table]
Values: [12, 18, 13]


Finding the Size of a MutableMap

We can determine the size of mutable map using two methods. By using the size property of the map and by using the count() method. 

Example:

Kotlin
fun main() {
    val map = mutableMapOf(1 to "A", 2 to "B", 3 to "C", 4 to "D")

    println("Size using size property: ${map.size}")
    println("Size using count() method: ${map.count()}")
}

Output: 

Size using size property: 4
Size using count() method: 4


Retrieving Values from MutableMap

We can retrieve values from a mutable map using different methods discussed in the below program. 

Example:

Kotlin
fun main() {
    val teams = mutableMapOf(1 to "India", 2 to "Australia", 3 to "England", 4 to "Africa")

    println("Team ranked #1 is: ${teams[1]}")
    println("Team ranked #3 is: ${teams[3]}")
    println("Team ranked #4 is: ${teams.get(4)}")
    println(teams.getOrDefault(2, "Unknown"))
}

Output: 

Team ranked #1 is: India
Team ranked #3 is: England
Team ranked #4 is: Africa
Australia


Adding Elements: put() and putAll()

The put() and putAll() function is used to add elements in the MutableMap.put() function adds single element at time while putAll() function can be used to add multiple element at a time in MutableMap.

Example: 

Kotlin
fun main() {
    val map = mutableMapOf<String, String>()
    map.put("Name", "Geek")
    map.put("Country", "India")
    
    println("<----Traverse mutableMap---->")
    for ((k, v) in map) {
        println("Key = $k, Value = $v")
    }
    
    val newMap = mapOf("Department" to "Computer Science", "Hobby" to "Coding")
    map.putAll(newMap)
    
    println("<----Traversal after putAll()---->")
    for ((k, v) in map) {
        println("Key = $k, Value = $v")
    }
}

Output: 

<----Traverse mutableMap---->
Key = Name, Value = Geek
Key = Country, Value = India
<----Traversal after putAll()---->
Key = Name, Value = Geek
Key = Country, Value = India
Key = Department, Value = Computer Science
Key = Hobby, Value = Coding


Removing Elements: remove()

Following methods are used to remove an element from a mutable map:

  1. remove(key): Removes the entry with the given key.
  2. remove(key, value): Removes the entry only if both key and value match.

Example:

Kotlin
fun main() {
    val map = mutableMapOf("Name" to "Geek", "Company" to "GeeksforGeeks", "Country" to "India")
    
    for ((k, v) in map) {
        println("Key = $k, Value = $v")
    }
    
    println("\nRemoving 'Country' key...")
    map.remove("Country")
    
    val removed = map.remove("Company", "GeeksforGeeks")
    println("Was the pair removed?: $removed")
    
    println("\n<---Traverse Again--->")
    for ((k, v) in map) {
        println("Key = $k, Value = $v")
    }
}

Output:

Key = Name, Value = Geek
Key = Company, Value = GeeksforGeeks
Key = Country, Value = India

Removing 'Country' key...
Was the pair removed?: true

<---Traverse Again--->
Key = Name, Value = Geek


Clearing All Elements: clear()

We can remove all entries from the map using the clear() function.

Example:

Kotlin
fun main() {
    val map = mutableMapOf("Name" to "Geek", "Company" to "GeeksforGeeks")
    
    for ((k, v) in map) {
        println("Key = $k, Value = $v")
    }
    
    map.clear()
    println("Map after clear(): $map")
}

Output:

Key = Name, Value = Geek
Key = Company, Value = GeeksforGeeks
Map after clear(): {}


Traversing a MutableMap

We can traverse (loop through) a MutableMap by using a for loop and entry set.

Example:

Kotlin
fun main() {
    val map = mutableMapOf(1 to "Aditya", 4 to "Vilas", 2 to "Manish", 3 to "Manjot")
    
    for ((key, value) in map) {
        println("Key = $key, Value = $value")
    }
}

Output: 

Key = 1, Value = Aditya
Key = 4, Value = Vilas
Key = 2, Value = Manish
Key = 3, Value = Manjot
Comment

Explore