Scala: Remove none elements from map and flatten

前端 未结 4 706
臣服心动
臣服心动 2021-01-01 16:02

I have a map:

Map(\"key1\" -> Some(\"value1\"), \"key2\" -> None, \"key3\" -> Some(\"value3\"))

I want to remove all None

4条回答
  •  醉梦人生
    2021-01-01 16:44

    My take using for comprehensions:

    val m = Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3"))
    for( (key,value) <- m if(value.isDefined)) yield (key,value.get)
    

提交回复
热议问题