How does Scala's mutable Map update [map(key) = newValue] syntax work?

后端 未结 3 905
温柔的废话
温柔的废话 2020-12-15 17:46

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         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 18:24

    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.

提交回复
热议问题