我正在尝试更新数组中的hashmap,但无法这样做。
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
}
}
}这是Combiner类
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)如上所述,在初始化这个case类之后,我该如何添加mapTypes,上面的更新代码似乎对我不起作用。
发布于 2017-02-25 03:46:55
如果想要使用更新代码,可以在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
}https://stackoverflow.com/questions/42445972
复制相似问题