Open In App

Kotlin mutableMapOf()

Last Updated : 07 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Kotlin MutableMap is an interface of collection frameworks which contains objects in the form of keys and values. It allows the user to efficiently retrieve the values corresponding to each key. The key and values can be of the different pairs like <Int, String>, < Char, String>, etc.
For using the MutableMap interface we need to use functions as shown below 
 

mutableMapOf() or mutableMapOf <K, V>()

For declaring MutableMap Interface
 

interface MutableMap<K, V> : Map<K, V> (source)

 

Mutable function example containing Entries, Keys, Values.

Kotlin




fun main(args: Array<String>) {
    val items = mutableMapOf("Box" to 12, "Books" to 18, "Table" to 13)
 
    println("Entries: " + items.entries)       //Printing Entries
    println("Keys:" + items.keys)              //Printing Keys
    println("Values:" + items.values)          //Printing Values
}


Output: 
 

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

 

Finding map Size –

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. 
 

Kotlin




fun main(args : Array<String>) {
 
    val ranks = mutableMapOf(1 to "India",2 to "Australia",
        3 to "England",4 to "Africa")
    //method 1
    println("The size of the mutablemap is: "+ranks.size)
    //method 2
    println("The size of the mutablemap is: "+ranks.count())
}


Output: 
 

The size of the mutablemap is: 4
The size of the mutablemap is: 4

 

Get values of Map –

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

Kotlin




fun main() {
 
    val ranks = mutableMapOf(1 to "India",2 to "Australia",
        3 to "England",4 to "Africa")
 
    //method 1
    println("Team having rank #1 is: "+ranks[1])
    //method 2
    println("Team having rank #3 is: "+ranks.getValue(3))
    //method 3
    println("Team having rank #4 is: "+ranks.getOrDefault(4, 0))
    // method  4
    val team = ranks.getOrElse(2 ,{ 0 })
    println(team)
}


Output: 
 

Team having rank #1 is: India
Team having rank #3 is: England
Team having rank #4 is: Africa
Australia

 

put() and putAll() function

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.
Kotlin program of using put() and putAll() function – 
 

Kotlin




fun main(args: Array<String>) {
    val mutableMap = mutableMapOf<String, String>()
    mutableMap.put("Name", "Geek")
    mutableMap.put("Country", "India")
 
    val map = mapOf<String,String>("Department" to "Computer Science",
        "Hobby" to "Coding")
 
    println("<----Traverse mutableMap---->")
    for (key in mutableMap.keys) {
        println("Key = "+key +", "+"Value = "+mutableMap[key])
    }
    mutableMap.putAll(map)
    println("<----Traversal after putting hashmap---->")
    for (key in mutableMap.keys) {
        println("Key = "+key +", "+"Value = "+mutableMap[key])
    }
}


Output: 
 

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

 

remove(key) and remove(key, value) function –

Function remove(key) is used to remove value which corresponds to its mentioned key. 
Function remove(key, value) is used to remove elements containing keys and values
Kotlin program to demonstrate remove() function – 
 

Kotlin




fun main(args: Array<String>) {
 
    val mutableMap = mutableMapOf<String, String>()
    mutableMap.put("Name", "Geek")
    mutableMap.put("Company", "GeeksforGeeks")
    mutableMap.put("Country", "India")
 
    for (key in mutableMap.keys) {
        println("Key = ${key}, Value = ${mutableMap[key]}")
    }
    println()
    println("Remove the Key: "+mutableMap.remove("Country"))
    println()
    println("Is pair removes from the map: "
            +mutableMap.remove("Company","GeeksforGeeks"))
    println()
    println("<---Traverse Again--->")
    for (key in mutableMap.keys) {
        println("Key = ${key}, Value = ${mutableMap[key]}")
    }
}


Output: 
 

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

Remove the Key: India

Is pair removes from the map: true

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

 

clear() function –

Used to remove all the element from the mutableMap.
Kotlin program of using clear() function – 
 

Kotlin




fun main(args: Array<String>) {
 
    val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
    mutableMap.put("Name", "Geek")
    mutableMap.put("Company", "GeeksforGeeks")
 
    for (key in mutableMap.keys) {
        println("Key = ${key}, Value = ${mutableMap[key]}")
    }
 
    println("mutableMap.clear()")
    println("Method called to clear the map: "+mutableMap.clear())
    println("Map Empty: "+mutableMap)
}


Output: 
 

Key = Name, Value = Geek
Key = Company, Value = GeeksforGeeks
mutableMap.clear()
Method called to clear the map: kotlin.Unit
Map Empty: {}

 

Traversal in a mutablemap –

Traversing means traveling through every node in the data structures like Linked List, Arrays, Trees, etc. 
It just means that traveling to every node at least once to display it to the user or perform an operation on it. 
To understand this let’s take an example below 
Kotlin program to demonstrate the traversal – 
 

Kotlin




fun main(args: Array<String>) {     //Declaring function
    //Creating MutableMap of different types
    val mutableMap = mutableMapOf(1 to "Aditya",
        4 to "Vilas", 2 to "Manish", 3 to "Manjot")
 
    for (key in mutableMap.keys) {
        println("Key = ${key}, Value = ${mutableMap[key]}")     
    }
}


Output: 
 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads