scala: union of two maps whose key type is the same and whose value type is a collection of elements, but whose types are different

后端 未结 3 1105
半阙折子戏
半阙折子戏 2020-12-20 06:35

I would like to create a union of two maps whose key type is the same and whose value type is a collection of elements, but whose types are different.

Consider the f

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-20 07:16

    This appears to work.

    val peopleToChildrenAndDogs: Map[String, (Seq[Child], Seq[Pet])] = {
      (peopleToChildren.keySet ++ peopleToPets.keySet).map { k =>
        k -> (peopleToChildren.getOrElse(k, Seq())
             ,peopleToPets.getOrElse(k, Seq()))
      }.toMap
    }
    

    Get all the keys. For every key do a getOrElse() on each of the feeder Maps.

提交回复
热议问题