0

The question is very simple, is there a way in scala to initialize a Mutable map with a inmutable map?

I want something like this:

import scala.collection.mutable.{ Map => MutableMap }
...
val dumbMap = Map("abc" -> 123)
val theMap = new MutableMap(dumbMap)

of course I could iterate or use the map function to initialize my mutable map and add all the elements in dumbMap, but... it think it would be ridiculous if scala lacks of such a basic method

SOLUTION EDIT: as mentioned in the previous question, it is not exactly a constructor, but a Method in its companion

val theMap = MutableMap[String, Integer](dumbMap.toSeq: _*)

Pay attention to the types of your map

Ordiel
  • 2,442
  • 3
  • 36
  • 52
  • 1
    The `scala.collection.mutable.Map` companion object has an `apply((A, B)*)` method ( http://www.scala-lang.org/api/2.11.4/index.html#scala.collection.mutable.Map$@apply[A,B](elems:(A,B)*):CC[A,B] ), so you can do the following: `val theMap = MutableMap(dumbMap)` (note: no `new` there). – Gábor Bakos May 01 '15 at 17:10
  • I have tried without the new word also and ... it is not working for me, I'll double check my sintax – Ordiel May 01 '15 at 17:12
  • @GáborBakos (A, B)* != Map[A,B]... this won't work. – Cubic May 01 '15 at 17:36

0 Answers0