Search code examples
scala

How to work with an Option[Set[A]] in Scala?


Given a toy example like:

val myMap = Map("a" -> Set(10, 20))
val myVal = 20

Is there a more concise way to do this?:

val myKey = "a" // imagine this comes from a loop
myMap.get(myKey) match {
  case Some(set) => set.contains(myVal)
  case None => false
}

I guess I could do this, but it doesn't seem idiomatic:

myMap.contains(myKey) && 
  selectedGsnsAtOdo(myKey).contains(myVal)

Related to this question


Solution

  • This will achieve the same result as the match expression:

    myMap.get(myKey).exists(_.contains(myVal))