Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin: get a list of some property values from a collection / list of objects
import kotlin.reflect.KMutableProperty1
inline fun <reified T, Y> MutableList<T>.arrayListOfField(property: KMutableProperty1<T, Y?>): ArrayList<Y> {
val output = ArrayList<Y>()
this.forEach {t: T ->
@Suppress("UNCHECKED_CAST")
output.add(property.get(t) as Y)
}
return output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment