Use kotlin's predicate find
to get ONE.
listOf("Hello", "Henry", "Alabama).find { it.startsWith("He") }
// Returns the first match of the list
If you want all of them matching a certain condition use filter
listOf("Hello", "Henry", "Alabama).filter { it.startsWith("He") }
// Returns "Hello" and "Henry"
So in your case the ideal thing to do would be to get a flat list of categories (including your subcategories; for this I reccomend the use of flatMap, flatten
or similar predicates.
// This way you just have an entire List<Category>
// This is a naive approach that assumes that subcategories won't have subcategories
val allCategories = categories.flatMap { cat -> listOf(cat) + cat.subcategories.orEmpty()
}
And finally do
allCategories.filter { cat -> cat.id in listOfIds }
You can read about all these predicates in the kotlin.collections package.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…