Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - ITERATING MAP


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.


Iterating a Map using 'for loop'


Example :



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)
    }
}


Output :



  The key is : 5 :: And the value is : Is a Number
  The key is : John :: And the value is : Is a Name
  The key is : Kotlin :: And the value is : Is a Language

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.

java_Collections

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)
}

1st Iteration


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.

java_Collections
java_Collections

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).

Output :



  The key is : 5 :: And the value is : Is a Number

2nd Iteration


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.

java_Collections

And the print statement, prints the key and value(i.e. John and Is a Name).

Output :



  The key is : 5 :: And the value is : Is a Number
  The key is : John :: And the value is : Is a Name

3rd Iteration


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.

java_Collections

And the print statement, prints the key and value(i.e. Kotlin and Is a Language).

Output :



  The key is : 5 :: And the value is : Is a Number
  The key is : John :: And the value is : Is a Name
  The key is : Kotlin :: And the value is : Is a Language

Now let us look at the second way of Iterating a Map.


Iterating the Map using 'forEach'


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)
    }
}


Output :



  5=Is a Number
  John=Is a Name
  Kotlin=Is a Language

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.

java_Collections

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.

Output :



  5=Is a Number
  John=Is a Name
  Kotlin=Is a Language

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.


Iterating the Map using 'forEach' and display the keys only


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)
    }
}


Output :



  5
  John
  Kotlin

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.


Iterating the Map using 'forEach' and display the values only


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.value)
    }
}


Output :



  Is a Number
  Is a Name
  Is a Language

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.


Iterating a Map using 'iterator()' and 'hasNext()' method using while loop


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()
        println(i)
    }
}


Output :



  5=Is a Number
  John=Is a Name
  Kotlin=Is a Language

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.

java_Collections

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.

java_Collections

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.

Output :



  5=Is a Number
  John=Is a Name
  Kotlin=Is a Language

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.


Iterating a Map using 'iterator()' and 'hasNext()' and printing the keys


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)
    }
}


Output :



  5
  John
  Kotlin

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.


Iterating a Map using 'iterator()' and 'hasNext()' and printing the values only


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().value
        println(i)
    }
}


Output :



  Is a Number
  Is a Name
  Is a Language

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.