Scala | Option

01 Apr, 2019
The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.
Important points :
The instance of an Option that is returned here can be an instance of Some class or None class in Scala, where Some and None are the children of Option class.
When the value of a given key is obtained then Some class is generated.
When the value of a given key is not obtained then None class is generated.
Example :
// Scala program for Option  // Creating objectobject option{      // Main method    def main(args: Array[String])    {          // Creating a Map        val name = Map("Nidhi" -> "author",                         "Geeta" -> "coder")          // Accessing keys of the map        val x = name.get("Nidhi")        val y = name.get("Rahul")          // Displays Some if the key is        // found else None         println(x)        println(y)    }}
Some(author)
None

Here, key of the value Nidhi is found so, Some is returned for it but key of the value Rahul is not found so, None is returned for it.
Using Pattern Matching :Example :// Scala program for Option// with Pattern matching  // Creating objectobject pattern{      // Main method    def main(args: Array[String])    {          // Creating a Map        val name = Map("Nidhi" -> "author",                         "Geeta" -> "coder")          //Accessing keys of the map        println(patrn(name.get("Nidhi")))        println(patrn(name.get("Rahul")))    }      // Using Option with Pattern     // matching    def patrn(z: Option[String]) = z match     {          // for 'Some' class the key for        // the given value is displayed        case Some(s) => (s)          // for 'None' class the below string         // is displayed        case None => ("key not found")    }}Output:author
key not found
Here, we have used Option with Pattern Matching in Scala.
Example :
// Scala program for Option// with Pattern matching  // Creating objectobject pattern{      // Main method    def main(args: Array[String])    {          // Creating a Map        val name = Map("Nidhi" -> "author",                         "Geeta" -> "coder")          //Accessing keys of the map        println(patrn(name.get("Nidhi")))        println(patrn(name.get("Rahul")))    }      // Using Option with Pattern     // matching    def patrn(z: Option[String]) = z match     {          // for 'Some' class the key for        // the given value is displayed        case Some(s) => (s)          // for 'None' class the below string         // is displayed        case None => ("key not found")    }}
author
key not found

Here, we have used Option with Pattern Matching in Scala.
getOrElse() Method:This method is utilized in returning either a value if it is present or a default value when its not present. Here, For Some class a value is returned and for None class a default value is returned.Example:// Scala program of using// getOrElse method  // Creating objectobject get{      // Main method    def main(args: Array[String])    {          // Using Some class        val some:Option[Int] = Some(15)          // Using None class        val none:Option[Int] = None           // Applying getOrElse method        val x = some.getOrElse(0)        val y = none.getOrElse(17)          // Displays the key in the         // class Some        println(x)          // Displays default value         println(y)    }}Output:15
17
Here, the default value assigned to the None is 17 so, it is returned for the None class.
// Scala program of using// getOrElse method  // Creating objectobject get{      // Main method    def main(args: Array[String])    {          // Using Some class        val some:Option[Int] = Some(15)          // Using None class        val none:Option[Int] = None           // Applying getOrElse method        val x = some.getOrElse(0)        val y = none.getOrElse(17)          // Displays the key in the         // class Some        println(x)          // Displays default value         println(y)    }}
15
17

Here, the default value assigned to the None is 17 so, it is returned for the None class.
isEmpty() Method:This method is utilized to check if the Option has a value or not.Example:// Scala program of using// isEmpty method  // Creating objectobject check{      // Main method    def main(args: Array[String])    {          // Using Some class        val some:Option[Int] = Some(20)          // Using None class        val none:Option[Int] = None           // Applying isEmpty method        val x = some.isEmpty        val y = none.isEmpty          // Displays true if there        // is a value else false        println(x)        println(y)    }}Output:false
true
Here, isEmpty returns false for Some class as it non-empty but returns true for None as its empty.
// Scala program of using// isEmpty method  // Creating objectobject check{      // Main method    def main(args: Array[String])    {          // Using Some class        val some:Option[Int] = Some(20)          // Using None class        val none:Option[Int] = None           // Applying isEmpty method        val x = some.isEmpty        val y = none.isEmpty          // Displays true if there        // is a value else false        println(x)        println(y)    }}
false
true

Here, isEmpty returns false for Some class as it non-empty but returns true for None as its empty.
Picked
Scala
scala-collection
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
For Loop in Scala
Scala | map() method
Scala | flatMap Method
String concatenation in Scala
Scala | reduce() Function
Type Casting in Scala
Scala List filter() method with example
Scala Tutorial – Learn Scala with Step By Step Guide
Scala String substring() method with example
Enumeration in Scala