gpt4 book ai didi

scala - 在斯卡拉清除 map

转载 作者:行者123 更新时间:2023-12-02 22:03:17 29 4
gpt4 key购买 nike

问题陈述:

当我尝试清除Map列时,处理程序Map的column部分也会被清除。有什么避免的方法吗?

def getTransformations(configuration: NodeSeq) {
var id: Int = 0
var methodCall: String = ""
val completeTransformation = Map[Int, Map[String, Map[String, String]]]()
val transformationsMap = Map[Int, Map[String, Map[String, String]]]()
val handlers = Map[String, Map[String, String]]()
val columns = Map[String, String]()

for (transformations <- configuration \\ "transformations") {
for (transformation <- transformations \\ "transformation") {
id += 1
for (handler <- transformation \\ "handler") {
for (input <- handler \\ "input") {
columns.+=(((input \\ "@name").mkString) -> input.text)
}
handlers(handler.attribute("type").mkString) = columns
columns.clear()
}
transformationsMap(id) = handlers
handlers.clear()
}
}
transformationsMap
}

例:
As seen in the image the handlers Map is build using the columns Map

When the columns.clear is executed, its value in handlers Map is also cleared

最佳答案

FWIW,在此处编写您要执行的操作的“scala”方式如下所示:

 val transformationsMap = (configuration \\ "transformations")
.flatMap(_ \\ "transformation")
.zipWithIndex
.map { case (trf, idx) => (idx+1) -> trf }
.toMap
.mapValues(_ \\ "handler)
.mapValues {
_.map { h => h.attribute("type").mkString -> (h \\ "input") }
.toMap
.mapValues {
_.map { in => (in \\ "@name".mkString) -> in.text }
.toMap
}
}

关于scala - 在斯卡拉清除 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46114179/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com