【发布时间】:2019-05-12 12:32:46
【问题描述】:
case class test(primary : String, secondary : Array[String], count : Int)
给定数组 a1:
a1: Array[test] = Array(test(Speed,Array(VR,ABC),5), test(Speed,Array.Empty[String],2), test(Speed,Array(Another,VR),3), test(Speed,Array(Another),3))
给定数组 a2:
a2: Array[test] = Array(test(Speed,Array(VR,ABC),6), test(Speed,Array.Empty[String],5), test(Speed,Array(Another),2), test(Speed,Array(SomethingElse),2))
我需要对计数求和并将其放在new Array[test] 中,按主要值和次要值分组。我该怎么做?这里计数是主要和次要组的总组合。这 2 个数组是从 2 个输入数据生成的统计信息。我的任务是汇总统计数据。
结果应该是:
a3: Array[test] = Array(test(Speed,Array(VR,ABC),11), test(Speed,Array.Empty[String],7),test(Speed,Array(Another),5),test(Speed,Array(Another,VR),3),test(Speed,Array(SomethingElse),2))
【问题讨论】:
-
也许尝试使用 zip 和 map 来访问案例类的 count 属性?
-
你可以对两个数组求和,按主次分组,你的键应该是一个元组,然后减少值求和,并返回所有减少的值。
标签: arrays scala collections