Last active
December 18, 2015 04:49
-
-
Save tototoshi/5727717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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