Site icon AndroidRide

Kotlin For each tutorial : 9 Examples

In this tutorial, you’ll learn how to use Kotlin’s forEach higher-order function.

When we need to iterate over an array or list to search for or filter specific data, the traditional for loop is often used. However, in Kotlin, there’s a more functional and concise approach—the forEach function.

Let’s create a simple example using forEach.

Simple forEach Example in Kotlin

val list = listOf(1, 2, 3)

    list.forEach { item ->
        println(item)
    }


kotlin list foreach

Range with forEach

In this part, we can use range with forEach. In kotlin, range means sequence of values defined between start value and end value.

Sometimes, you don’t need to create a list. You can use a range directly like below, which is more memory-efficient.

(1..3).forEach {
        println(it)
    }

range foreach kotlin

Using forEach with a string

If you’re a beginner, you may encounter coding questions in interviews such as counting characters in a string. in these situations, forEach function can be quite useful.

val charArray = "String"
    charArray.forEach {
        println(it)
    }

Using forEach with a string

Using forEach with array

val array = arrayOf("a","b","c")
    array.forEach {
        println("array : $it")
    }


array foreach kotlin

Using forEach with Maps

  
    val map = mapOf(1 to "a", 2 to "b", 3 to "c")

    map.forEach{ (key, value) ->
        println("Key: $key -> Value: $value")
    }

map foreach kotlin

Using forEachIndexed with list

forEach, which comes with an index, is called forEachIndexed.

If you are creating a list and need to display each item with its index, you can use this higher-order function.

val list = listOf(1, 2, 3)
 list.forEachIndexed { index, item ->
        println("Index: $index, Item: $item")
    }

list forEachIndexed

Using forEachIndexed with map

val map = mapOf(1 to "a", 2 to "b", 3 to "c")
  map.entries.forEachIndexed{ index, item ->
        println("Index: $index, Key: ${item.key} -> Value: ${item.value}")
    }

map forEachIndexed

forEach – continue functionality

You can use continue statement directly in a for loop, but you cannot use it directly in forEach.

val list = listOf(1, 2, 3)

 list.forEach {
        if(it==2){
            println("Skips the second iteration")
            return@forEach
        }

        println(it)
    }

foreach continue kotlin

forEach – break functionality

val list = listOf(1, 2, 3)

  run loop@{
        list.forEach {
            if(it==2){
                println("Skips iterations from here")
                return@loop
            }

            println(it)
        }
    }

break foreach kotlin

Complete Source Code

package com.androidride.kotlin

fun main() {

    val list = listOf(1, 2, 3)

    println("simple list - forEach example")
    list.forEach {
        println(it)
    }

    println("range with foreach")
    (1..3).forEach {
        println(it)
    }

    println("string with foreach")
    val charArray = "String"
    charArray.forEach {
        println(it)
    }

    println("array with foreach")
    val array = arrayOf("a","b","c")
    array.forEach {
        println("array : $it")
    }

    println("map with foreach")
    val map = mapOf(1 to "a", 2 to "b", 3 to "c")

    map.forEach{ (key, value) ->
        println("Key: $key -> Value: $value")
    }

    println("forEachIndexed with lists")
    list.forEachIndexed { index, item ->
        println("Index: $index, Item: $item")
    }

    println("forEachIndexed with maps")
    map.entries.forEachIndexed{ index, item ->
        println("Index: $index, Key: ${item.key} -> Value: ${item.value}")
    }

    println("forEach - break functionality")
    run loop@{
        list.forEach {
            if(it==2){
                println("Skips iterations from here")
                return@loop
            }

            println(it)
        }
    }

    println("forEach - continue functionality")
    list.forEach {
        if(it==2){
            println("Skips the second iteration")
            return@forEach
        }

        println(it)
    }

}

Exit mobile version