There are many ways by which we can Iterate a Map.
So we have a Map with key and values,
'5' --> 'Is a Number' 'John' --> 'Is a Name' 'Kotlin' --> 'Is a Language'
Now, let us see the first and easiest way to iterate a Map.
fun main() { var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") for ((key,value) in x) { println("The key is : "+key+" :: And the value is : "+value) } }
Similarly, in the above code we have created a Map using the mapOf() method.
var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language")
And initialised to the variable x.
In the next line we have used the for loop to Iterate through the Map.
for ((key,value) in x) { println("The key is : "+key+" :: And the value is : "+value) }
Now, if we see the iterations of for loop,
for ((key,value) in x) { println("The key is : "+key+" :: And the value is : "+value) }
In the first Iteration, the first key and value pair of the Map x (i.e. 5 and Is a Number) is taken and put into the variables key and value in the for loop.
And the print statement,
println("The key is : "+key+" :: And the value is : "+value)
prints the value of key and value(i.e. 5 and Is a Number).
Similarly, in the second iteration, the second key and value pair of the Map x (i.e. John and Is a Name) is taken and put into the variables key and value in the for loop.
And the print statement, prints the key and value(i.e. John and Is a Name).
Similarly, in the third Iteration the third key and value pair of the Map x (i.e. Kotlin and Is a Language) is taken and put into the variables key and value in the for loop.
And the print statement, prints the key and value(i.e. Kotlin and Is a Language).
Now let us look at the second way of Iterating a Map.
fun main() { var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") x.forEach { println(it) } }
Similarly, in the above code we have created a Map using the mapOf() method.
var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language")
And initialised to the variable x.
In the next line we have used the forEach loop to Iterate through the Map.
x.forEach { println(it) }
Now, if you see the println() statement, there is a variable named it.
Well! it is a special type of variable that is used with forEach loop.
Just place it inside forEach loop and the it variable will be displaying the key and value pairs of the Map one by one.
Now, we have seen the variable it printed both the key and value as output.
Now, what if, we wanted to print the key only. Or just the value.
We can do that as well. Let us see in the below example.
fun main() { var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") x.forEach { println(it.key) } }
So, if you take a look at the output, only keys are printed.
All thanks to it.key in the print statement.
println(it.key)
key is a special type of variable that is used with it to print the key only.
Similarly, let us see how we can print the value only.
fun main() { var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") x.forEach { println(it.value) } }
So, if you take a look at the output, only values are printed.
And this time, all thanks to it.value in the print statement.
println(it.value)
value is also a special type of variable that is used with it to print the value only.
Next, let us look at the third way to Iterate over a Map.
fun main() { var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") val iter = x.iterator() while (iter.hasNext()) { val i = iter.next() println(i) } }
Similarly, in the above code we have created a Map using the mapOf() method.
var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language")
And initialised to the variable x.
Now, to use, iterator() and hasNext(), we have created a variable, iter and initialised with x.iterator().
And internally what happens is, Kotlin would make the Map ready for iteration.
val iter = x.iterator()
And iter variable would contain the values of the Map that can be Iterated.
Then we have used the while loop to Iterate the Map.
while (iter.hasNext()) { val i = iter.next() println(i) }
In the first line of while loop, we have used the hasNext() method. That would check if all the values are Iterated or not.
while (iter.hasNext()) { ... ... }
Once all the values are iterated, the control comes out of the while loop.
Now, the question would be, how does Kotlin understands that all the values of the Map are Iterated.
Well! Thanks to the next() method.
val i = iter.next()
The next() method takes the next value from the iter(iter is holding the Map currently) variable and prints on the screen.
while (iter.hasNext()) { val i = iter.next() println(i) }
Now, if we see the output. Both the key and value is printed as output.
Now, what if, we wanted to print the key only. Or just the value.
We can do that as well. Let us see in the below example.
fun main() { var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") val iter = x.iterator() while (iter.hasNext()) { val i = iter.next().key println(i) } }
So, if you take a look at the output, only keys are printed.
All thanks to iter.next().key.
val i = iter.next().key
key is a special type of variable that is used with iter.next() to print the key only.
Similarly, let us see how we can print the value only.
fun main() { var x = mapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language") val iter = x.iterator() while (iter.hasNext()) { val i = iter.next().value println(i) } }
So, if you take a look at the output, only values are printed.
And this time,
val i = iter.next().value
We have used value variable with iter.next() to print the values.
Next, let us see, how to insert the elements in a Map.