If I understood you correct, you want to find a Category
within categoriesAndProducts
based on Category.name
. Having this Category
(which is the key), you then want to get it's Boolean
value from categoriesAndProducts
.
You can do this by filtering first for keys that are matching the Category name:
val key: Category? = categoriesAndProducts.keys.firstOrNull {
it.name == "someName"
}
categoriesAndProducts[key] // returns null when key is null
You could write an extension function that helps you accessing the Category key based on a name:
fun Map<Category, Boolean>.findByName(name: String): Boolean? {
val key: Category? = keys.firstOrNull { it.name == name }
return get(key)
}
categoriesAndProducts.findByName("someName") // returns boolean
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…