To convert a list of strings to a list of integers in Kotlin, you can use the map function along with the toIntOrNull function to transform each string to its integer representation.
In this example,
stringList
.map()
function to apply a transformation to each string in the list.toIntOrNull()
function to convert each string to its integer representation.fun main() {
val stringList = listOf("1", "2", "3", "4", "5")
val intList = stringList.map { it.toIntOrNull() }
.filterNotNull()
println("List of integers: $intList")
}
List of integers: [1, 2, 3, 4, 5]
In this tutorial, we learned How to Convert a List of Strings to a List of Integers in Kotlin language with well detailed examples.