cool breeze cool breeze - 1 year ago 82
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download