kingsley
11/04/2018, 4:50 PMif an entry isn’t in the base map, it won’t exist in the final oneYes. Which is why I iterated through
mapOverride
instead. This is a union of both maps with the assumption that they only contain sets/other values and a key always points to values of similar type (or null) in both maps. Entries in mapOverride
are always preferred except for sets where both will get merged
I suppose that is what you’re trying to achieve?dave08
11/04/2018, 5:01 PMMap<String, Set<String>>
... and all these erased types makes it more complicated than it should be...kingsley
11/04/2018, 5:07 PMI have one more type to add here:You mean this as input? TheMap<String, Set<String>>
mergeWith
will handle it as wellone of the three typesI’m assuming this is
String
, Set<String>
and null
? 🤔dave08
11/04/2018, 5:09 PMkingsley
11/04/2018, 5:09 PMdave08
11/04/2018, 5:10 PMkingsley
11/04/2018, 5:13 PMfun MutableMap<String, Any>.mergeWith(mapOverride: Map<String, Any>?) {
mapOverride?.forEach { (k, v) ->
val baseValue = this[k]
this[k] = when {
baseValue is Set<*> && v is Set<*> -> baseValue + v
baseValue is Map<*, *> && v is Map<*, *> -> baseValue + v
else -> v
}
}
}
+
in both cases. That’s fine, since the operands are of different types, the operator resolves to different methods, as it applies to Set
and Map
accordinglydave08
11/04/2018, 5:17 PMkingsley
11/04/2018, 5:17 PMdave08
11/04/2018, 5:20 PMkingsley
11/04/2018, 5:20 PM