Let us say, we have a Map that contains,
As Key and Value pair.
Now, let us say, we want to update the value of the Key, 5 with Is an one digit number.
5, Is an one digit number
Where 5 is the Key and Is an one digit number is the new Value.
We can update the entries of a Map using two ways:
Let us see in the below example using the first way.
fun main() { var x = mutableMapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") x[5] = "Is an one digit number" println(x) }
So, in the above code we have created a Map using mutableMapOf() and Key and Value pairs.
var x = mutableMapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language")
And initialised to the variable x.
Now, we are supposed to update the value of the Key, 5 with Is an one digit number.
So, we have used the below way to update it.
x[5] = "Is an one digit number"
And the entry for the Key 5 is updated in the Map.
And we get the below output,
Let us see the second way of updating the Key Value using the put() Function.
fun main() { var x = mutableMapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") x.put(5, "Is an one digit number") println(x) }
So, in the above code we have created a Map using mutableMapOf and Key and Value pairs.
var x = mutableMapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language")
And initialised to the variable x.
Now, we are supposed to update the value of the Key, 5 with Is an one digit number.
So, we have used the below way to update it.
x.put(5, "Is an one digit number")
And the entry for the Key 5 is updated in the Map.
And we get the below output,