in Education by
I am a newbie to Scala and I want to learn how can I add null and empty check on an optional string? val myString : Option[String] if (null != myString) { myString .filter(localStr=> StringUtils.isEmpty(localStr)) .foreach(localStr=> builder.queryParam("localStr", localStr)) } Above code works but I want to learn some elegant way of writing same check. Any help is highly appreciated, thanks JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
There is a quite convenient way using the Option constructor. scala> Option("Hello Worlds !") res: Option[String] = Some(Hello Worlds !) scala> Option(null) res: Option[Null] = None So if you would have list of Strings with possible null values you can use a combination of Option and flatten: scala> List("Speak", "friend", null, "and", null ,"enter").map(s => Option(s)) .flatten res: List[String] = List(Speak, friend, and, enter)

Related questions

0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    I have a table which contains 'NULL' values which are of type 'Datetime'. Now i have to convert ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, ... not as good. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have a fileupload option in my project. It inclueds a query which returns a dataset. It works fine ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    suppose I have passed : list=[] How can I check if a list is empty or not? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How do you check for an empty (zero Element) array?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    What are the best practices if you have a class which accepts some parameters but none of them are allowed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 5, 2022 in Education by JackTerrance
0 votes
    You can check to see whether an R object is NULL with the _________ function. (a) is.null() (b) is. ... and Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    I have two strings both with value null. But when I am comparing the string I have some problem : ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 16, 2022 in Education by JackTerrance
0 votes
    ANSI-standard SQL allows the use of special operators in conjunction with the WHERE clause. A special ... Schemes topic in chapter Concurrency Control of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    In the code below, in "parse" function I am trying to get substring from the string "line". I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    In the code below, in "parse" function I am trying to get substring from the string "line". I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    PHP's explode function returns an array of strings split on some provided substring. It will return empty ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
...