And*_*mov 1 collections kotlin
我有一些data clases
data class Data(val docNumber: Int?,
val docType: Int?,
val fiscprops: List<FiscProp>,
val name: String?
) {
fun getFiscProp(tag: Int) = fiscprops.firstOrNull { it.tag == tag }
}
data class FiscProp(val caption: String?,
val printable: String?,
val tag: Int?,
val value: Any?,
val fiscprops: List<FiscProp>?)
Run Code Online (Sandbox Code Playgroud)
我需要FiscProp在tag嵌套中找到lists。如果我使用,getFiscProp我只能找到FiscProp它是否位于层次结构的第一级。
我怎样才能找到各个级别的元素?不知道会是多少级。
val FiscProp.allProps: Sequence<FiscProp>
get() = sequence {
yield(this@allProps)
fiscprops?.forEach {
yieldAll(it.allProps)
}
}
class Data(...) {
fun getFiscProp(tag: Int) = fiscprops.asSequence()
.flatMap { it.allProps}
.firstOrNull { it.tag == tag }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
683 次 |
| 最近记录: |