Last active
August 29, 2015 14:17
-
-
Save fancellu/6f555fd31df13c560098 to your computer and use it in GitHub Desktop.
Combines 2 maps of sequences. Have seen this asked a few times. This is a fairly concise solution.
This file contains hidden or 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
def combine[K,V](map1:Map[K,Seq[V]], map2:Map[K,Seq[V]]) = | |
(map1.toList ++ map2.toList).groupBy(_._1).mapValues(_.map(_._2).flatten).map(identity) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The map(identity) is to force the map view to strict map. http://stackoverflow.com/questions/14882642/scala-why-mapvalues-produces-a-view-and-is-there-any-stable-alternatives