我想在Scala中进行总结。
我有一个案例类实例列表。
case class Structure(structureFirst:String,structureSecond:String,structureThird:String,count:Int)
val structures = List(
Structure("first001","second001","third001",1),
Structure("first002","second002","third002",2),
Structure("first002","second002","third002",2),
Structure("first003","second003","third003",3),
Structure("first003","second003","third003",3),
Structure("first003","second003","third003",3),
Structure("first004","second004","third004",4),
Structure("first004","second004","third004",4),
Structure("first004","second004","third004",4),
Structure("first004","second004","third004",4)
)
然后我想像这个SQL一样总结。
SELECT SUM(Structure.count) FROM structures GROUP BY Structure.structureFirst,Structure.structureSecond,Structure.structureThird;
这是输出。
val result = List(
Structure("first001","second001","third001",1),
Structure("first002","second002","third002",4),
Structure("first003","second003","third003",9),
Structure("first004","second004","third004",16)
)
我如何总结这个?
感谢。
答案 0 :(得分:0)
我绝不是世界上最好的Scala开发者,但我的新手尝试屈服这个
val result = for (structs <- structures.groupBy(_.structureFirst).values.toList)
yield {
structs.reduce((item, cur) => item.copy(count=item.count + cur.count))
}
result: List[Structure] = List(
Structure(first002,second002,third002,4),
Structure(first004,second004,third004,16),
Structure(first001,second001,third001,1),
Structure(first003,second003,third003,9)
)
答案 1 :(得分:0)
使用groupBy
,在这几乎是一行中:
structures.groupBy(s => (s.structureFirst, s.structureSecond, s.structureThird)).map {
case (key, ss) =>
ss.reduce((a,b) => a.copy(count = a.count + b.count) )
}.toList.
sortBy (_.count)