Is there an equivalent in Kotlin for Scala's Traversable.collect function?
Specifically, I would like to map a List to a List, but only if that can succeed.
myList.map {
val myNullableThing = it.someNullableMethod()
// If it is null, then I cannot continue here
// and I want to simply ignore this item
}
Although less generalized than the Scala collect
function, mapNotNull
should work in your case: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-not-null.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With