Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin equivalent of Scala Traversable.collect

Tags:

kotlin

scala

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
}
like image 452
Alexander Weickmann Avatar asked Oct 14 '25 14:10

Alexander Weickmann


1 Answers

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

like image 86
dasmaze Avatar answered Oct 18 '25 14:10

dasmaze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!