我是Kotlin的新手,我想將一個對象(ProductVisibility)映射到另一個對象(fmpProduct)上。某些對象無法轉換,因此我需要在某些情況下跳過它們。在Kotlin中檢查地圖函數中的null
我想知道是否有更好的方法來做到這一點,而不是我用過濾器和「!!」做的事情。我覺得它被黑了。我錯過了什麼嗎?
val newCSProductVisibility = fmpProducts
.filter { parentIdGroupedByCode.containsKey(it.id) }
.filter { ProductType.fromCode(it.type) != null } //voir si on accumule les erreus dans une variable à montrer
.map {
val type = ProductType.fromCode(it.type)!! //Null already filtered
val userGroupIds = type.productAvailabilityUserGroup.map { it.id }.joinToString(",")
val b2bGroupIds = type.b2bUserGroup.map { it.id }.joinToString { "," }
val b2bDescHide = !type.b2bUserGroup.isEmpty()
val parentId = parentIdGroupedByCode[it.id]!! //Null already filtered
CSProductDao.ProductVisibility(parentId, userGroupIds, b2bGroupIds, b2bDescHide)
}
編輯:更新贊評論地圖訪問建議
要讀取地圖值,你應該使用數組註解來替代:parentIdGroupedByCode [it.id] – BladeCoder
我更新地圖,就像你說的感謝訪問它,但它仍然可空 – Mike