2014-12-04 1 views
0

나는 아래의 데이터 List 수집이 있습니다클래스를지도 [X, (List [Y], Z)]로 변환하려면 어떻게해야합니까?

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 모음으로 매핑하는 것입니다 : GROUPBYmapValues ​​

"S00001" -> ((1,2), "3000.0") 
"S00002" -> ((3), "3000.0") 
"S00003" -> ((4), "4000.0") 

답변

1

예.

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)) 

이렇게하면됩니다. snake_case 대신 CamelCase를 사용하도록 스칼라/Java 모범 사례를 존중하도록 이름을 변경했습니다.

관련 문제