我是 Kotlin 的新手,想要根据另一个对象(fmpProduct)映射一个对象(ProductVisibility)。有些对象无法转换,因此我需要在某些条件下跳过它们。
我想知道是否有比我用过滤器和 "!!" 更好的方法。我觉得这样做有点 hack。我是不是漏掉了什么?
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)
}
编辑:根据评论建议更新了地图访问方式