Traverse Set elements and get values using them as keys from a hashmap using Scala
<p>I am trying to use a set created using first column of all files and to create hashmap for each files using first column as key and the second column as value. </p>

<p>Using this Set, I want to check the values for the hashmap but if there is no such key in that hashmap for that file, it needs to put "0" as the values for that key in the new hashmap. It needs to have new hashmap for each file. </p>

<pre><code>//Set for storing 
var ids : Set[String] = collection.immutable.HashSet()
//Hashmap for storing 
    var id:Map[String,String] = collection.immutable.Map()

    for (arg&lt;-args){
    ids ++= Source.fromFile(arg)
    .getLines()
    .filterNot(_.trim.startsWith("#"))
    .map(_.split("\t")(0))
    }

   //Create hash map for each file
   for (arg&lt;-args){
      id ++= Source.fromFile(arg).getLines()
      .filterNot(_.trim.startsWith("#"))
      .map { l =&gt; 
       val Array(k,v1,_*)= l.split("\t") 
       k-&gt; (v1)}.toMap
         val filtered = id.filter(i =&gt;     
        ids.contains(i._1))
        println(filtered)                   
    }
</code></pre>

<p>For example File a, </p>

<pre><code>#comments
ABC 2
ABN 7
CVF 9
</code></pre>

<p>File b</p>

<pre><code>#Comments
#
#
ABC 1
DFG 2
CVF 3
</code></pre>

<p>Output:</p>

<pre><code>Map(ABC -&gt; 2, ABN -&gt; 7, CVF -&gt; 9)
Map(ABC -&gt; 1, ABN -&gt; 7, CVF -&gt; 3, DFG -&gt; 2)
</code></pre>

<p>Desired output:</p>

<pre><code>Map(ABC -&gt; 2, ABN -&gt; 7, CVF -&gt; 9,DFG -&gt; 0)
Map(ABC -&gt; 1, ABN -&gt; 0, CVF -&gt; 3, DFG -&gt; 2)
</code></pre>