Mutable MultiMap to immutable map

I am creating a MultiMap

 val ms = new collection.mutable.HashMap[String, collection.mutable.Set[String]]() with collection.mutable.MultiMap[String, String] 

which, after filling with records, should be passed to a function waiting for Map[String, Set[String]] . toMap transfer does not work directly and tries to convert it to an immutable card via toMap

 ms.toMap[String, Set[String]] 

gives

 Cannot prove that (String, scala.collection.mutable.Set[String]) <:< (String, Set[String]). 

Can this be solved without manually repeating in ms and inserting all the records into a new immutable card?

+4
source share
2 answers

The problem seems to be changed. Thus, turning into immutable sets works:

 scala> (ms map { x=> (x._1,x._2.toSet) }).toMap[String, Set[String]] res5: scala.collection.immutable.Map[String,Set[String]] = Map() 

Or even better, following the suggestion of Daniel Gathered:

 scala> (ms mapValues { _.toSet }).toMap[String, Set[String]] res7: scala.collection.immutable.Map[String,Set[String]] = Map() 
+5
source

How to use mapValues to change only Set ?

+2
source

All Articles