我得到了一个List[(Int,String,Double)]的查询结果,我需要将它转换成一个Map[String,String] (用于在html选择列表中显示)
我的黑客解决方案是:
val prices = (dao.getPricing flatMap {
case(id, label, fee) =>
Map(id.toString -> (label+" $"+fee))
}).toMap一定有更好的方法来达到同样的效果。
发布于 2012-05-28 21:35:21
简明扼要一点:
val prices =
dao.getPricing.map { case (id, label, fee) => ( id.toString, label+" $"+fee)} toMap更短的替代方案:
val prices =
dao.getPricing.map { p => ( p._1.toString, p._2+" $"+p._3)} toMap发布于 2012-05-28 21:29:53
这个怎么样?
val prices: Map[String, String] =
dao.getPricing.map {
case (id, label, fee) => (id.toString -> (label + " $" + fee))
}(collection.breakOut)collection.breakOut方法提供了一个CanBuildFrom实例,该实例确保即使从List进行映射,也会重新构造Map,这要归功于类型注释,并且避免了创建中间集合。
https://stackoverflow.com/questions/10785372
复制相似问题