【发布时间】:2016-07-20 12:45:01
【问题描述】:
我有一个可以归为不同类别的属性列表。值类型不同。但不同的类别可能具有相同的值类型。
val data: List[(String, Any)] = List("first name" -> "rockie",
"last name" -> "yang",
"address" -> "rather not reveal",
"wanted age" -> 23)
val publicProps = Set("first name", "last name")
val privateProps = Set("address", "wanted age")
val filtered = data.filter(d => publicProps.contains(d._1))
// but the type of filtered is List[(String, Any)]
// I could do a map to convert the type to List[(String, String)]
filtered.map{case (name, value: String) => name -> value}
// If the number of publicProps is small
// I could do collect and convert at the same type
data.collect {
case ("first name", value: String) =>
("first name", value)
case ("last name", value: String) =>
("last name", value)
}
有没有一种优雅的方式来收集和转换同一类型的类型?
【问题讨论】:
标签: scala collections