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




KOTLIN - ITERATING SET


There are many ways by which we can Iterate a Set.


So we have a Set with values, '5', 'John' and 'Kotlin'.


Now, let us see the first and easiest way to iterate a Set.


Iterating a Set using 'for loop'


Example :



fun main() {
    var x = setOf(5, "John", "Kotlin")

    for (i in x) {
        println(i)
    }
}


Output :



  5
  John
  Kotlin

Similarly, in the above code we have created a 'Set' using the 'setOf()' method.


var x = setOf(5, "John", "Kotlin")

And initialised to the variable 'x'.

java_Collections

In the next line we have used the 'for loop' to Iterate through the 'Set'.


for (i in x) {
	println(i)
}

Now, if we see the iterations of for loop,


for (i in x) {
	println(i)
}

1st Iteration


In the first Iteration the first value of the 'Set' 'x' (i.e. 5) is taken and put into the variable 'i'.

java_Collections

And the print statement, prints the value of 'i'.

Output :



  5

2nd Iteration


Similarly, in the second Iteration the second value of the 'Set' 'x' (i.e. 'John') is taken and put into the variable 'i'.

java_Collections

And the print statement, prints the value of 'i'.

Output :



  5
  John

3rd Iteration


Similarly, in the third Iteration the third value of the 'Set' 'x' (i.e. 'Kotlin') is taken and put into the variable 'i'.


And the print statement, prints the value of 'i'.

Output :



  5
  John
  Kotlin

Now, if you see the final output. You can find that the values of the 'Set' are displayed in the same way they were inserted.


i.e. First '5' is printed, then the name 'John' and finally 'Kotlin' is printed.


And if you see the Set,


x = [5, "John", "Kotlin"]

It is printed in the same order it was inserted. And this is why a Set is said to be 'Ordered'.


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


Iterating a Set using 'forEach' method


Example :



fun main() {
    var x = setOf(5, "John", "Kotlin")

    x.forEach {
        println(it)
    }
}


Output :



  5
  John
  Kotlin

Similarly, in the above code we have created a 'Set' using the 'setOf()' method.


var x = setOf(5, "John", "Kotlin")

And initialised to the variable 'x'.

java_Collections

In the next line we have used the 'forEach' loop to Iterate through the 'Set'.


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 elements of the 'Set' one by one.


Next, let us look at the third way to Iterate over a 'Set'.


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


Example :



fun main() {
    var x = setOf(5, "John", "Kotlin")

    val iter = x.iterator()
    
    while (iter.hasNext()) {
        val i = iter.next()
        println(i)
    }
}


Output :



  5
  John
  Kotlin

Similarly, in the above code we have created a 'Set' using the 'setOf()' method.


var x = setOf(5, "John", "Kotlin")

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 'Set' ready for iteration.


val iter = x.iterator()

And 'iter' variable would contain the values of the 'Set' that can be Iterated.

java_Collections

Then we have used the 'while' loop to Iterate the Set.


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 Set 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 Set currently) variable and prints on the screen.


while (iter.hasNext()) {
	val i = iter.next()
	println(i)
}

Next, let us see, how to access the elements of the Set in the next tutorial.