What is the best way to merge two maps, given an associative function when keys are present in both maps.
Here is my current implementation
private def mergeMap[A, B](map1: concurrent.Map[A, B], map2: concurrent.Map[A, B], f: (B, B) => B) : concurrent.Map[A, B] = {
val keys = map1.keys ++ map2.keys
val newMap = new TrieMap[A, B]()
keys.foreach { k =>
(map1.get(k), map2.get(k)) match {
case (Some(v1), Some(v2)) => newMap.put(k, f(v1, v2))
case (Some(v1), None) => newMap.put(k, v1)
case (None, Some(v2)) => newMap.put(k, v2)
case _ => // could not happen
}
}
newMap
}