Adding element to a scala set which is a map value

scala

Solution

You're probably using `immutable.Map`. You need to use `mutable.Map`, or replace the set instead of modifying it with another immutable map.

Here's a reference of a description of the mutable vs immutable data structures.

So...

import scala.collection.mutable.Map
var m = Map[Int,Set[Int]]()
m += 1 -> Set(1)
m(1) += 2

Problem

I have the following map in Scala: ``` var m = Map[Int,Set[Int]]() m += 1 -> Set(1) m(1) += 2 ``` I've discovered that the last line doesn't work. I get "error: reassignment to val". So I tried ``` var s = m(1) s += 2 ``` Then when I compared `m(1)` with `s` after I added 2 to it, their contents were different. So how can I add an element to a set which is the value of a map? I come from a Java/C++ background so what I tried seems natural to me, but apparently it's not in Scala.

Original source