我有Map[key:String, value:String]
和List[(s1:String,s2:String,s3:String)]
我想从整个Row(元组)中获取(提取)第3个字符串第3个字段s3。
主要是它与地图中的键和列表中的3字段行进行一对一映射。
答案 0 :(得分:0)
如果您想要在地图中提取其他字段时在元组中提取字段,
.filter
通过列表才能匹配Map
和例如,
scala> val customers = Map("customer1" -> "location",
"customer2" -> "location2",
"customer-strange" -> "canada-strange")
customers: scala.collection.immutable.Map[String,String] = Map(customer1 -> location, customer2 -> location2, customer-strange -> canada-strange)
scala> val orders = Seq(("customer1", "shirts", "pants"),
("customer2", "guitar", "drum"),
("customer-not-in-map", "whatever-1", "whatever-2"))
orders: Seq[(String, String, String)] = List((customer1,shirts,pants), (customer2,guitar,drum), (customer-not-in-map,whatever-1,whatever-2))
scala> orders.collect { case (s1, s2, s3) if customers.contains(s1) => s3 }
res2: Seq[String] = List(pants, drum)
另一个版本是
scala> orders.filter { case (s1, s2, s3) => customers.contains(s1) }
.map { case (s1, s2, s3) => s3}
res5: Seq[String] = List(pants, drum)
尝试工作示例 - https://scastie.scala-lang.org/prayagupd/Hz4BDU02RQm1AzJZl73lAA/5