Skip to content

Instantly share code, notes, and snippets.

@tototoshi
Last active December 18, 2015 04:49
Show Gist options
  • Save tototoshi/5727717 to your computer and use it in GitHub Desktop.
Save tototoshi/5727717 to your computer and use it in GitHub Desktop.
val m1 = Map(1 -> 2, 2 -> 5, 3 -> 1)
val m2 = Map(2 -> 4, 3 -> 3, 4 -> 3)
def toMultiMap[A, B](m1: Map[A, B], m2: Map[A, B]): Map[A, Set[B]] =
(m1.toSeq ++ m2.toSeq).groupBy(_._1).mapValues(_.map(_._2).toSet)
/*
scala> toMultiMap(m1, m2).mapValues(_.max)
res0: scala.collection.immutable.Map[Int,Int] = Map(2 -> 5, 4 -> 3, 1 -> 2, 3 -> 3)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment