wei
10/03/2018, 4:44 PMkeys and another map of key -> value map, whose keys are a subset of keys and whose value is either SOFT_DELETED or NOT_DELETED. For example:
val keys = listOf("key1", "key2", "key3")
val map = mapOf("key1" to "SOFT_DELETED", "key2" to "NOT_DELETED")
I need to combine keys and map to generate another map finalMap, whose keys will contain all keys from keys that are not in keys of map with values of DELETED and keys of map that has a value of SOFT_DELETED. In the above example, I’d have:
val finalMap = mapOf("key1" to "SOFT_DELETED", "key3" to "DELETED")
I can think of mapping over keys and check each key against map and generate finalMap, like:
val finalMap = keys.mapNotNull {
when(map[it]) {
"SOFT_DELETED") -> it to "SOFT_DELETED"
null -> it to "DELETED"
else -> null
}
}
Is there a more idiomatic way in kotlin to do this?Egor Trutenko
10/03/2018, 4:55 PMval finalMap = keys.filter { map[it] != "NOT_DELETED" }
.mapNotNull { it to map[it] ?: "DELETED" }wei
10/03/2018, 5:05 PM