title: unexpected side effect of map and for loop in scala
body: <p>Perhaps a simple to be answered question, but I did not find a satisfying answer from the API. I am not trying to write nice code, but I am trying to learn more on how certain things work: </p>
  
  <p>I have created an initial HashMap. From an arbitrary list I would like to use map in order to create a list of HashMaps. I add a (key, value) pair to my HashMap within the .map function. Thus, in any successive index from this list the size of the stored HashMap should increase by one (so list(i).size == list(i+1).size - 1 for every i).</p>
  
  <p>But with the following code snipped I get a list of Maps which are all equal (namely the full HashMap). If I print out the growing initial HashMap everything seems right, though.</p>
  
  <pre><code>scala&gt; import scala.collection.mutable.HashMap
  
  val m = new HashMap[Int, Int]
  
  List(1,2,3,4) map {e =&gt;
      println(m)
      val newM = m += e -&gt; (e+2)
      newM
  }
  Map()
  Map(1 -&gt; 3)
  Map(1 -&gt; 3, 2 -&gt; 4)
  Map(3 -&gt; 5, 1 -&gt; 3, 2 -&gt; 4)
  
  import scala.collection.mutable.HashMap
  m: scala.collection.mutable.HashMap[Int,Int] = Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4)
  res7: List[scala.collection.mutable.HashMap[Int,Int]] = List(Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4), Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4), Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4),     Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4))
  </code></pre>
  
  <p>What I expected was something like </p>
  
  <p>List(Map(1 -> 2), Map(1 -> 2, 2 -> 4), Map(1 -> 2, 2 -> 4, 3 -> 5), Map(1 -> 2, 2 -> 4, 3 -> 5, 4 -> 6))</p>
  
  <p>The equivalent (I assume it is) version using a generative for loop results in exactly the same result:</p>
  
  <pre><code>scala&gt; for {
      i &lt;- 1 to 4
      val newM = m += i -&gt; (i+2)
  } yield newM
   res10: scala.collection.immutable.IndexedSeq[scala.collection.mutable.HashMap[Int,Int]] = Vector(Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4), Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4), Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4), Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4))
  
  scala&gt; for (i &lt;- 1 to 4) yield {
      val newM = m += i -&gt; (i+2)
      println(m)
      newM
  } 
  Map(1 -&gt; 3)
  Map(1 -&gt; 3, 2 -&gt; 4)
  Map(3 -&gt; 5, 1 -&gt; 3, 2 -&gt; 4)
  Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4)
  res11: scala.collection.immutable.IndexedSeq[scala.collection.mutable.HashMap[Int,Int]] = Vector(Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4), Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4), Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4), Map(3 -&gt; 5, 4 -&gt; 6, 1 -&gt; 3, 2 -&gt; 4))
  </code></pre>
  
  <p>I suppose I am missing a fundamental step, but I just do not see which. I appreciate any further help!</p>
  
  <p>A subsequent question: Obviously the order in which the (key, value) pairs are printed out is always the same (though not in order of how they were elements, but I do not expect this from a Map anyways, this is natural). What happens in the background when the pairs are stored that this always seems to be the case?</p>