scala better syntax for map getOrElse

前端 未结 4 1874
南方客
南方客 2021-02-05 10:29

is there a better way to write the code below?

val t = map.get(\'type).getOrElse(\"\"); 
if (t != \"\") \"prefix\" + t;

be interested in inline

4条回答
  •  悲&欢浪女
    2021-02-05 10:56

    It's also worth noting that, in certain cases, you can replace multiple common .getOrElse usages with one .withDefaultValue call.

    val map = complexMapCalculation().withDefaultValue("")
    
    val t = map('type)
    

    I wouldn't say this is something that should be done every time, but it can be handy.

提交回复
热议问题