title: Map withDefault of an empty ListBuffer in scala
body: <p>I'm trying to create a mutable <code>Map</code> with a default that creates a new <code>ListBuffer</code> when an element is requested that is not already in the map. However, while the new map is returned as the default it does not remain in the map. Maybe this is just how it works, I thought, but when I tested it with an <code>Int</code> rather than a <code>ListBuffer</code> it did exactly as I wanted. Here's some code to explain what I mean - what am I doing wrong?</p>
  
  <p>First, here it is working with a <code>Map[Int]</code>:</p>
  
  <pre><code>scala&gt; val a = collection.mutable.Map(1 -&gt; 1).withDefault(i =&gt; 0)
  a: scala.collection.mutable.Map[Int,Int] = Map(1 -&gt; 1)
  
  scala&gt; a(1) += 1 // adding to an existing element works as expected
  
  scala&gt; a
  res48: scala.collection.mutable.Map[Int,Int] = Map(1 -&gt; 2)
  
  scala&gt; a(2) += 1 // what about adding to a non-existing element?
  
  scala&gt; a // the new element has been added to the map
  res50: scala.collection.mutable.Map[Int,Int] = Map(1 -&gt; 2, 2 -&gt; 1)
  </code></pre>
  
  <p>Now with a <code>Map[ListBuffer[Int]]</code>:</p>
  
  <pre><code>scala&gt; val b = collection.mutable.Map(1 -&gt; collection.mutable.ListBuffer[Int]()).withDefault(i =&gt; collection.mutable.ListBuffer.empty[Int])
  b: scala.collection.mutable.Map[Int,scala.collection.mutable.ListBuffer[Int]] = Map(1 -&gt; ListBuffer())
  
  scala&gt; b(1) += 1 // appending to an existing element works as expected
  res51: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1)
  
  scala&gt; b
  res52: scala.collection.mutable.Map[Int,scala.collection.mutable.ListBuffer[Int]] = Map(1 -&gt; ListBuffer(1))
  
  scala&gt; b(2) += 1 // but appending to a non-existing element...
  res53: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1)
  
  scala&gt; b // leaves the map unchanged
  res54: scala.collection.mutable.Map[Int,scala.collection.mutable.ListBuffer[Int]] = Map(1 -&gt; ListBuffer(1))
  </code></pre>