我想检索键,该键存在于另一个列表/数组(假设B)中的列表/数组的值(假设A)
val B: List[String] = List("key1","key3") //I can refactor the type if needed
val paramNames: Array[String] = parameterNames // ["key1", "key2", "key3"]
val paramValues: Array[AnyRef] = args // [1, "value", Obj]
val A: Array[(String,AnyRef)] = paramNames.zip(paramValues) // [("key1", 1), ("key2", "value"), ("key3", Obj)]
//now I want to retrieve from A, all keys exist in B with their values
//to get [("key1", 1), ("key3", Obj)]
答案 0 :(得分:4)
只需使用过滤器:
val C = A.filter(k => B.contains(k._1))
这只会得到元组,其键包含在B
中。