我的问题如下:
for (something <- someotherthing) {
val m = in._2 // gets a scala.collection.mutable.Map[String, String] with already some values in it
if (someotherotherthing) {
m("item1") = "reset"
m("item3") = "reset"
}
something :+= m
}
我经历了一些事情,得到了一个字符串键值对的映射。我想在某些情况下仅重置item1
和item3
。但是,当我运行此功能时,添加到something
的所有地图都会包含这两个resets
。我可以将此地图复制到其他地方,然后添加它以便我不会立即编辑所有地图吗?
答案 0 :(得分:2)
所以这里有一些帮助!
val (someThing, inMap) = yourTuple
if (someBooleanCondition) {
inMap.map {
case (key, _) if key == "item1" => "item1" -> "reset"
case (key, _) if key == "item3" => "item3" -> "reset"
case (key, value) => key -> value
}
}
这对你有用吗?我的例子中有一些概念是:
我建议您在编写Scala之前先了解这些概念!从长远来看,它会对你有所帮助!