I\'m working through Cay Horstmann\'s Scala for the Impatient book where I came across this way of updating a mutable map.
scala> val scores = scala.coll
Can you try this: => to update list of Map
import java.util.concurrent.ConcurrentHashMap
import scala.collection.JavaConverters._
import scala.collection.concurrent
val map: concurrent.Map[String, List[String]] = new ConcurrentHashMap[String, List[String]].asScala
def updateMap(key: String, map: concurrent.Map[String, List[String]], value: String): Unit = {
map.get(key) match {
case Some(list: List[String]) => {
val new_list = value :: list
map.put(key, new_list)
}
case None => map += (key -> List(value))
}
}
The problem is you're trying to update immutable map. I had the same error message when my map was declared as
var m = new java.util.HashMap[String, Int]
But when i replaced the definition by
var m = new scala.collection.mutable.HashMap[String, Int]
the m.update
worked.
This is an example of the apply
, update
syntax.
When you call map("Something")
this calls map.apply("Something")
which in turn calls get
.
When you call map("Something") = "SomethingElse"
this calls map.update("Something", "SomethingElse")
which in turn calls put
.
Take a look at this for a fuller explanation.