Concept Flow - It keyword for single parameter
Start Lambda
Single Parameter?
Yes
Use 'it' as parameter
Execute Lambda Body
Return Result
End
When a lambda has one parameter, Kotlin lets you use 'it' to refer to it without naming it explicitly.
val numbers = listOf(1, 2, 3) val doubled = numbers.map { it * 2 } println(doubled)
| Step | Lambda Input (it) | Operation | Result | Output List State |
|---|---|---|---|---|
| 1 | 1 | 1 * 2 | 2 | [2] |
| 2 | 2 | 2 * 2 | 4 | [2, 4] |
| 3 | 3 | 3 * 2 | 6 | [2, 4, 6] |
| 4 | - | End of list | - | [2, 4, 6] |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| it | - | 1 | 2 | 3 | - |
| doubled | [] | [2] | [2, 4] | [2, 4, 6] | [2, 4, 6] |
Kotlin lambdas with one parameter can use 'it' implicitly.
No need to name the parameter explicitly.
Use 'it' inside the lambda body to refer to the parameter.
Example: list.map { it * 2 } doubles each element.
If more than one parameter, 'it' cannot be used.it keyword represent in a Kotlin lambda with a single parameter?itit automatically refers to that single parameter inside the lambda.it = single lambda parameter [OK]it means the one lambda input [OK]it is a loop variableit with a keyword to declare variablesit prints outputit in a Kotlin lambda that doubles each number in a list?map has one parameter, so it can be used implicitly.list.map { it * 2 } correctly uses it without naming the parameter explicitly.it only when lambda has one parameter [OK]it togetherval numbers = listOf(1, 2, 3)
val result = numbers.filter { it > 1 }
println(result)it > 1, so only numbers greater than 1 remain.it > 1 = [2, 3] [OK]it meets condition [OK]val list = listOf(1, 2, 3)
val doubled = list.map { it * }
println(doubled)it * is incomplete; it lacks a value after the multiplication operator.*.it * is valid aloneit must be declared explicitlyval words = listOf("apple", "banana", "cat"), which Kotlin code uses it correctly to create a list of word lengths greater than 3?words.filter { it.length > 3 }, correctly filters the words list to include only words with length greater than 3, using it properly.it.length > 3 to get words longer than 3 [OK]it incorrectly in filterPass the test and this pattern moves unknown