Kotlin List forEach

Kotlin List foreach is used perform the given action on each item of the list. Inside the code block of forEach, the item could be referenced as it.

Syntax – List forEach

theList.forEach {

print(it)

}

Example – Kotlin List forEach – String

In the following example, we shall print each item of String List using forEach.

Example – Kotlin List forEach – Integer

In the following example, we shall print each item of an Integer List using forEach.

Example – Kotlin List forEach – Custom Object

In the following example, we shall consider list of Data Class objects and print a message for each item of the List using forEach.

Conclusion

In this Kotlin TutorialKotlin list forEach, we have learnt how to perform a block of statement for each element of the list, with examples.