java - Scala Check optional string is null or empty -


i newbie scala , want learn how can add null , empty check on optional string?

val mystring : option[string]  if (null != mystring) {   mystring     .filter(localstr=> stringutils.isempty(localstr))     .foreach(localstr=> builder.queryparam("localstr", localstr)) } 

above code works want learn elegant way of writing same check. highly appreciated,

thanks

there quite convenient way using option constructor.

scala> option("hello worlds !") res: option[string] = some(hello worlds !)   scala> option(null) res: option[null] = none 

so if have list of strings possible null values can use combination of option , flatten:

scala> list("speak", "friend", null, "and", null ,"enter").map(s => option(s)) .flatten res: list[string] = list(speak, friend, and, enter) 

Comments