cool breeze cool breeze - 16 days ago 8
Scala Question

Remove keys that don't contain any of the values in a set

val s = Set("blue", "orange")
val m = Map("product_orange_123" -> 1, "prodoct_blue_123" -> 2, "product_green_123" -> 5, "product_blue_887" -> 7)


I want to remove any key in the map that doesn't contain any of the values in the set s.

Expected output:

("product_orange_123" -> 1, "prodoct_blue_123" -> 2, "product_blue_887" -> 7)

Answer

Well I guess what you meant

Filter keys of map that does not contains any value of the set

m.filterKeys(key => s.exists(key.contains(_)) )

this will do