将 List[(String,String)] 转换为 Map[String, Map[String,String]]
Convert List[(String,String)] to Map[String, Map[String,String]]
假设我有以下列表[(String, String)]:
List((recap_items[4].invoice_items[0].id,6),
(recap_items[4].invoice_items[1].id,7),
(recap_items[4].invoice_items[1].qty,1),
(recap_items[4].invoice_items[0].qty,1),
(recap_items[4].invoice_items[1].sur_key,19),
(recap_items[4].invoice_items[0].sur_key,17))
如何将该列表转换为下面的地图?
Map(
recap_items[4].invoice_items[0] -> Map(id -> 6, qty -> 1, sur_key -> 17),
recap_items[4].invoice_items[1] -> Map(id -> 7, qty -> 1, sur_key -> 19)
)
或者有没有更好的表示来表示 List 这样的组?
编辑
case class Recap(recap_id: String, recap_date: Date, submitted_id:String, edited_id: String, recap_items: List[Recap_items])
case class Recap_items(product_name: String, product_id: String, qty: Int, unit_name: String, unit_multiplier: Int, sys_qty: Int, invoice_items: List[Invoice_items])
case class Invoice_items(sur_key: Long, id: Long, qty: Int)
当前方法
下面是我目前的方法,它给了我 Map[String, List[String]]:
代码:
flash.data.filterKeys(_.startsWith("recap_items["+i+"].invoice_items")).toList.sortBy(x => x._1).map{
x => (x._1.split("""\.""").toList(1), x._2)
}.groupBy(_._1).mapValues{
x => x.map( v => v._2)
}
输出:
Map(invoice_items[1] -> List(7, 1, 19),
invoice_items[0] -> List(6, 1, 17))
任何改进此代码的线索?
不确定为什么要将所有内容都转换为字符串(我的意思是,recap_items[4]。invoice_items[1] 而不是实际的项目名称)。
如果您的输入是一个 Recap
对象,那么您可以这样做:
val recap: Recap = ...
val map: Map[String, Map[String, String]] =
(for (
(recapItem, recapItemIndex) <- recap.recap_items.zipWithIndex;
(invoiceItem, invoiceItemIndex) <- recapItem.invoice_items.zipWithIndex
) yield {
s"recap_item[$recapItemIndex]invoiceItem[$invoiceItemIndex]" -> Map("id" -> s"$invoiceItem.id", "qty" -> s"$invoiceItem.qty", "sur_key" -> s"$invoiceItem.sur_key")
}).toMap
假设我有以下列表[(String, String)]:
List((recap_items[4].invoice_items[0].id,6),
(recap_items[4].invoice_items[1].id,7),
(recap_items[4].invoice_items[1].qty,1),
(recap_items[4].invoice_items[0].qty,1),
(recap_items[4].invoice_items[1].sur_key,19),
(recap_items[4].invoice_items[0].sur_key,17))
如何将该列表转换为下面的地图?
Map(
recap_items[4].invoice_items[0] -> Map(id -> 6, qty -> 1, sur_key -> 17),
recap_items[4].invoice_items[1] -> Map(id -> 7, qty -> 1, sur_key -> 19)
)
或者有没有更好的表示来表示 List 这样的组?
编辑
case class Recap(recap_id: String, recap_date: Date, submitted_id:String, edited_id: String, recap_items: List[Recap_items])
case class Recap_items(product_name: String, product_id: String, qty: Int, unit_name: String, unit_multiplier: Int, sys_qty: Int, invoice_items: List[Invoice_items])
case class Invoice_items(sur_key: Long, id: Long, qty: Int)
当前方法
下面是我目前的方法,它给了我 Map[String, List[String]]:
代码:
flash.data.filterKeys(_.startsWith("recap_items["+i+"].invoice_items")).toList.sortBy(x => x._1).map{
x => (x._1.split("""\.""").toList(1), x._2)
}.groupBy(_._1).mapValues{
x => x.map( v => v._2)
}
输出:
Map(invoice_items[1] -> List(7, 1, 19),
invoice_items[0] -> List(6, 1, 17))
任何改进此代码的线索?
不确定为什么要将所有内容都转换为字符串(我的意思是,recap_items[4]。invoice_items[1] 而不是实际的项目名称)。
如果您的输入是一个 Recap
对象,那么您可以这样做:
val recap: Recap = ...
val map: Map[String, Map[String, String]] =
(for (
(recapItem, recapItemIndex) <- recap.recap_items.zipWithIndex;
(invoiceItem, invoiceItemIndex) <- recapItem.invoice_items.zipWithIndex
) yield {
s"recap_item[$recapItemIndex]invoiceItem[$invoiceItemIndex]" -> Map("id" -> s"$invoiceItem.id", "qty" -> s"$invoiceItem.qty", "sur_key" -> s"$invoiceItem.sur_key")
}).toMap