Better way of converting a Map[K, Option[V]] to a Map[K,V]

后端 未结 3 1474
借酒劲吻你
借酒劲吻你 2021-01-01 15:31

I have some code that is producing a Map where the values are Option types, and I really of course want a map containing only the real values.

相关标签:
3条回答
  • 2021-01-01 15:35
    for ((k, Some(v)) <- input) yield (k, v)
    

    It's franza's answer from a later question, but it deserves a re-post here.

    0 讨论(0)
  • 2021-01-01 15:49
    input flatMap {case(k,ov) => ov map {v => (k, v)}}
    
    0 讨论(0)
  • 2021-01-01 15:57
    input.collect{case (k, Some(v)) => (k,v)}
    
    0 讨论(0)
提交回复
热议问题