简体   繁体   English

如何将类转换为Map [X,(List [Y],Z)]?

[英]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.

相关问题 如何将List [Tuple2 [X,Y]]转换为Map [X,List [Y]],以避免在Scala中出现重复项? - How to transform a List[Tuple2[X,Y]] to a Map[X, List[Y]] to avoid duplicates in Scala? 如何解决Scala编译器的“类X继承类Y [Z]的不同类型实例”的错误? - How to solve Scala compiler's “class X inherits different type instances of class Y[Z]” error? 如何将案例类转换为Map(在Scala.js中) - How can I convert a case class to a Map (in Scala.js) 如何调试错误“缺少X所需的类文件。 软件包Z的参考值Y引用了Scala中不存在的符号? - How to debug the error “Class file needed by X is missing. reference value Y of package Z refers to nonexisting symbol” in Scala? 如何像清单中那样在Scala中将方法名称X映射到方法名称Y - How to map method name X to method name Y in Scala like in List Scala:我有一个Set [X]和函数(X)=> Future [Y]并想输出一个Future [Map [X,Y]] - Scala: I have a Set[X] and function (X) => Future[Y] and want to output a Future[Map[X, Y]] 播放-数据类型Map [X,List [Y]]的Json反序列化 - Play - Json deserialization of data type Map[X,List[Y]] 猫效应:如何将 Map[x,IO[y]] 转换为 IO[Map[x,y]] - cats-effect:How to transform Map[x,IO[y]] to IO[Map[x,y]] 是否可以使用reduceByKey((x,y,z)=>…)? - Is it possible to user reduceByKey((x, y, z) => …)? 当 X 从 Y 扩展并且 Y 是特征时,如何在 Play JSON 中为 Y 和 List[X] 定义格式? - How to define format for both Y and List[X] in Play JSON when X extends from Y and Y is trait?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM