Easy Tutorial
❮ Anonymous Functions Scala Install ❯

Scala Option (Option)

Scala Collections

The Scala Option type is used to represent that a value is optional (has a value or has no value).

Option[T] is a container for an optional value of type T: If the value exists, Option[T] is a Some[T], and if it does not exist, Option[T] is the object None.

Let's look at a piece of code next:

// Although Scala can define the type of a variable without specifying, for clarity, I still
// explicitly define it here
 
val myMap: Map[String, String] = Map("key1" -> "value")
val value1: Option[String] = myMap.get("key1")
val value2: Option[String] = myMap.get("key2")
 
println(value1) // Some("value1")
println(value2) // None

In the above code, myMap is a hash map with a Key type of String and a Value type of String, but the difference is that its get() method returns a category called Option[String].

Scala uses Option[String] to tell you: "I will try to return a String, but there may also be no String to give you."

There is no data for key2 in myMap, and the get() method returns None.

Option has two subclasses, one is Some, and the other is None. When it returns Some, it means that the function has successfully given you a String, and you can get that String through the get() function. If it returns None, it means there is no string to give you.

Another example:

Example

object Test {
   def main(args: Array[String]) {
      val sites = Map("tutorialpro" -> "www.tutorialpro.org", "google" -> "www.google.com")
      
      println("sites.get( \"tutorialpro\" ) : " +  sites.get( "tutorialpro" )) // Some(www.tutorialpro.org)
      println("sites.get( \"baidu\" ) : " +  sites.get( "baidu" ))  //  None
   }
}

Executing the above code, the output is:

$ scalac Test.scala 
$ scala Test
sites.get( "tutorialpro" ) : Some(www.tutorialpro.org)
sites.get( "baidu" ) : None

You can also use pattern matching to output the matched value. An example is as follows:

Example

object Test {
   def main(args: Array[String]) {
      val sites = Map("tutorialpro" -> "www.tutorialpro.org", "google" -> "www.google.com")
      
      println("show(sites.get( \"tutorialpro\")) : " +  
                                          show(sites.get( "tutorialpro")) )
      println("show(sites.get( \"baidu\")) : " +  
                                      show(sites.get( "baidu")) )
   }
   
   def show(x: Option[String]) = x match {
      case Some(s) => s
      case None => "?"
   }
}

Executing the above code, the output is:

$ scalac Test.scala 
$ scala Test
show(sites.get( "tutorialpro")) : www.tutorialpro.org
show(sites.get( "baidu")) : ?

getOrElse() Method

You can use the getOrElse() method to get the existing element in the tuple or use its default value, as shown in the following example:

Example

object Test {
   def main(args: Array[String]) {
      val a:Option[Int] = Some(5)
      val b:Option[Int] = None 
      
      println("a.getOrElse(0): " + a.getOrElse(0) )
      println("b.getOrElse(10): " + b.getOrElse(10) )
   }
}

Executing the above code, the output is:

$ scalac Test.scala 
$ scala Test
a.getOrElse(0): 5
b.getOrElse(10): 10

isEmpty() Method

You can use the isEmpty() method to check whether the element in the tuple is None, as shown in the following example:

Example

object Test {
   def main(args: Array[String]) {
      val a:Option[Int] = Some(5)
      val b:Option[Int] = None 
      
      println("a.isEmpty: " + a.isEmpty )
      println("b.isEmpty: " + b.isEmpty )
   }
}

Executing the above code

❮ Anonymous Functions Scala Install ❯