By Alvin Alexander. Last updated: November 12, 2018
It’s a little hard to move back and forth between Scala and Kotlin because of some of the differences between the languages. Skipping the long story, here’s an example of how to print every line in a list of strings in Kotlin using forEach
and println
. First the setup:
import java.io.File fun readFile(filename: String): List<String> = File(filename).readLines() val lines = readFile("/etc/passwd")
Then here are two different ways to use forEach
with println
:
lines.forEach{ println(it) } lines.forEach{ line -> println(line) }
If you needed to see how to use forEach
with println
in Kotlin, I hope this is helpful.