A Map is a Collection that can also hold multiple values in Key and Value pairs. In the Map, the elements are unordered, unindexed, changeable and doesn't allow duplicate values.
You can think of a Map like an actual English Dictionaries. Where you search for a word and you get its explanation.
The declaration of a Map is also quite simple. You can place multiple values inside the mapOf() method in the form of Key and Value pairs.
Just imagine the Key to be the word you are going to search in the English Dictionary. And the Value is the explanation you find in it.
fun main() { var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") println(x) }
So, in the above code we have created a Map using mapOf() method. And the values are represented in Key and Value pairs.
The Key is a number or a String and in the value we have provided its explanation(Somewhat like the English Dictionary).
i.e. We know that the first element 5 is a number. So, in the Key, we have put the number 5 and in its value, we have put the explanation, Is a Number. Same logic is applicable for the other two Keys, John and Kotlin.
A Key and Value is separated by keyword to.
var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language")
And initialised to the variable x.
And in the next line we have printed the Map using the print statement.
println(x)
Now, if we see the output,
The output contains Key and Value separated by =.
But just remember, while we declare a Map, the Key and Value is separated by keyword to.