Skip to content

Instantly share code, notes, and snippets.

@Danilo-Araujo-Silva
Created October 31, 2018 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Danilo-Araujo-Silva/e2fac770bec3c4828090b4f0b5c7a7a7 to your computer and use it in GitHub Desktop.
Save Danilo-Araujo-Silva/e2fac770bec3c4828090b4f0b5c7a7a7 to your computer and use it in GitHub Desktop.
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