Kotlin basics : Using Map in the code - devrath/KotlinAlchemy GitHub Wiki
About Map
- A
Map
is a part of a collection framework where you provide a key
as an input and you get a corresponding value
as an output.
Map
is also called a dictionary.
When to use Map
and when to use MutableMap
Map
- > This is used when we have a fixed set of elements and it won't modify at runtime.
MutableMap
-> We use this when the collection has to be modified at runtime
- If you try to fetch a value for a key where the key does not exist, we get a null reply
private fun initiate() {
val listOfCities = mapOf(
1 to "Bangalore",
2 to "Mumbai",
3 to "Chennai",
)
println(listOfCities[1])
val listOfNames = mutableMapOf(
1 to "Manish",
2 to "John",
3 to "Sam",
)
listOfNames[4] = "Anudeep"
println(listOfNames[4])
}
}