title: scala var failed to get appended value outside foreach loop
body: <p>I have a text file named vertexDict which has multiple key value pairs splitted by "\1",and i need to convert this to a Map in my spark program.Here is my code:    </p>
  
  <pre><code>var mapDict = Map[Long, String]()
  sc.textFile(vertexDict).map(line=&gt; line.split("\1")).foreach(a=&gt;{
    if(a.length ==2 ){
      mapDict += a(0).toLong-&gt;a(1)
    }
  })
  </code></pre>
  
  <p>problem is mapDict is still empty after the foreach loop end,but i debuged into the loop, and mapDict successfully added the elements into it;so I bypass this problem by:</p>
  
  <pre><code>val mapDict = sc.textFile(vertexDict).map(line=&gt; line.split("\1")).map(a=&gt;a(0).toLong-&gt;a(1)).collect().toMap
  </code></pre>
  
  <p>it works.But i still don't know why foreach failed to add.
  thanks!</p>