Created
October 31, 2018 14:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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