我有一个val指向从HDFS文件中读取的大量记录。我们称之为'a'。 'a'有一堆记录都包含这3个属性:SRC,DEST,ACT。我需要复制'a',但在每条记录中交换SRC和DEST键的值。我如何在scala中执行此操作?我尝试了不同的地图功能变体,但似乎无法使其正常工作。
答案 0 :(得分:2)
嗯,没有代码示例,我猜测你的需求和先决条件,但是这样的事情可能有用:
case class Record(src: String, dest: String, act: String)
val a = List(
Record("srcA", "destA", "actA"),
Record("srcB", "destB", "actB"),
Record("srcC", "destC", "actC"),
Record("srcD", "destD", "actD"),
Record("srcE", "destE", "actE"),
)
val b = a.map(r => Record(r.dest, r.src, r.act))
println(a)
// => List(Record(srcA,destA,actA), Record(srcB,destB,actB), Record(srcC,destC,actC), Record(srcD,destD,actD), Record(srcE,destE,actE))
println(b)
// => List(Record(destA,srcA,actA), Record(destB,srcB,actB), Record(destC,srcC,actC), Record(destD,srcD,actD), Record(destE,srcE,actE))