[英]How can I convert a class to Map[X, (List[Y], Z)]?
I have List
collection with data below: 我有以下数据的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")
Is it possible to map it into below Map
collection: 是否可以将其映射到下面的Map
集合中:
"S00001" -> ((1,2), "3000.0")
"S00002" -> ((3), "3000.0")
"S00003" -> ((4), "4000.0")
Yes with groupBy an mapValues . 是的,使用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 ))
This should work. 这应该工作。 I changed the naming to honor the Scala/Java best practices to use CamelCase instead of snake_case. 我更改了名称,以纪念使用CamelCase而不是snake_case的Scala / Java最佳实践。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.