科特林。在嵌套列表中按参数查找元素

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)

我需要FiscProptag嵌套中找到lists。如果我使用,getFiscProp我只能找到FiscProp它是否位于层次结构的第一级。

我怎样才能找到各个级别的元素?不知道会是多少级。

IR4*_*R42 5

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)