Scala: Remove none elements from map and flatten

前端 未结 4 717
臣服心动
臣服心动 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:35

    Using partition over the map, like this,

    val (flattened,_) = map.partition(_._2.isDefined)
    

提交回复
热议问题