9

Possible Duplicate:
How can I convert immutable.Map to mutable.Map in Scala?

How do I create a new mutable map with the contents of the immutable map in Scala?

So far I have tried:

val m:scala.collection.mutable.Map[Int, Double] = scala.collection.mutable.Map[Int, Double](imm.map({case(key, value) => (key -> value) }))

to no avail.

Community
  • 1
  • 1
dave
  • 12,406
  • 10
  • 42
  • 59

2 Answers2

28
val im = Map(1->1.0, 2->3.0)
val mm = collection.mutable.Map[Int,Double]() ++= im
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • 4
    `++` creates another collection, while `++=` adds to the one you've got. The latter is less wasteful given that the collection is mutable. – Rex Kerr Jan 29 '12 at 11:59
10
val immM = Map(1 -> 2)
val mutM = collection.mutable.Map(immM.toSeq: _*)
Blaisorblade
  • 6,438
  • 1
  • 43
  • 76