我收集了以下数据的List
:
case class Expense_detail(po_id: Long, supplier_id: String, price: String)
Expense_detail(1,"S00001","1000.0"),
Expense_detail(2,"S00001","2000.0"),
Expense_detail(3,"S00002","3,000.0"),
Expense_detail(4,"S00003","4,000.0")
是否可以将其映射到下面的Map
集合中:
"S00001" -> ((1,2), "3000.0")
"S00002" -> ((3), "3000.0")
"S00003" -> ((4), "4000.0")
答案 0 :(得分:1)
是 groupBy mapValues 。
case class ExpenseDetail(poId: Long, supplierId: String, price: String)
val details : List[ExpenseDetail] = ...
details.
groupBy( _.supplierId ).
mapValues( details => ( (details.map(_.poId)), details.map(_.price.toInt).sum ))
这应该有效。 我更改了命名以尊重Scala / Java最佳实践,以使用CamelCase而不是snake_case。