2017-02-24 103 views
0

我正在尝试更新数组中的哈希映射,但无法这样做。哈希映射在scala中追加

def incrementCount(combiners: Array[Combiner], row: Row): Array[Combiner] = { 
    val tempMapTypes = new scala.collection.mutable.HashMap[String, (String, Array[String])] 
    for (i <- 0 until row.length) { 
     val array = getMyType(row(i), tempMapTypes) 
     for (elem <- tempMapTypes) { 
     combiners(i).mapTypes.update(elem._1,elem._2) <<<< this update doesnt work for me, always mapTypes is empty 
     } 
    } 
    } 

这是合班

case class Combiner(<other variables>, mapTypes: mutable.HashMap[String, (String, Array[String])]) 
     combiners 
     } 

这是怎么回事,它的另一种方法得到了初始化...

val mapTypes = new scala.collection.mutable.HashMap[String, (String, Array[String])] 
combiners += new Combiner(....,mapTypes) 

正如上面提到的,这种情况下的初始化之后类,如何追加mapTypes,上述更新代码似乎不适合我。

回答

0

如果你想用你的更新代码,你可以对Combiner定义++=方法:

class Combiner(..., mapTypes: mutable.HashMap[String, (String, Array[String])]) { 

    ... 

    def ++=(otherMapTypes: mutable.HashMap[String, (String, Array[String])]): Unit = 
    this.mapTypes ++= otherMapTypes 

} 

然后你就可以做到以下几点:

for(i <- row.length) { 
    combiners(i) ++= tempMapTypes 
}