The Scala Option (option) type is used to indicate that a value is optional (with or without value).
Option[T] is a container of optional values of type T: if the value exists Option[T] . It’s just one. Some[T] , if it does not exist Option[T] is the object None .
Next, let’s look at a piece of code:
// Although Scala may not define the type of variables, for clarity, I still // Define what he displays 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 type of ‘Key’ that is String , and Value is a type of hash map that is String . However, the difference is that its get() returns a category called Option[String] .
Scala usage Option[String] to tell you: I’ll find a way to send one back String , but maybe not, but there may not be a String for you.
There is no key2 data in myMap , but the ‘get()’ method returns’ None ‘
Option has two subcategories, one is Some , another one is None when he sent it back Some represents that this function successfully gives you a String and you can pass through get() , this function gets that. String if he returns None , it means there is no string to give you
Another example: Execute the above code, and the output is as follows: You can also output matching values through pattern matching. Examples are as follows: Execute the above code, and the output is as follows: You can use Execute the above code, and the output is as follows: You can use Execute the above code, and the output is as follows: The following table lists the Scala Serial number Method and description 1 Get optional value 2 Check whether the optional type value is None. If so, return true, otherwisereturn false. 3 Returns the number of elements, A (xroom1,…, xroomk), returns k 4 Gets the specified option, starting with 0. That is, A (x, n < k), returns x _ (n < 1), 0 < n < k. 5 Returns false if the element specified in the condition in the option existsand does not return true for None. 6 If the option contains a value and the conditional function passed to filterreturns true, filter returns the Some instance. Otherwise, the return valueis None. 7 If the option contains a value and the conditional function passed to filterreturns false, filter returns the Some instance. Otherwise, the return value is None. 8 If the option contains a value, it is passed to the function f for processing and returned, otherwise it returns None 9 If the option contains values, each value is passed to the function f, otherwise it is not processed. 10 If the option contains a value, returns the option value, otherwise returns the default value set. 11 Returns true if the optional value is an instance of Some, otherwise returnsfalse. 12 If the option contains a value, iterate over the optional value. Returns an empty iterator if the optional value is empty. 13 If the option contains a value, return the Some processed by the function f,otherwise return None 14 If an Option is None, the orElse method returns the value of the name parameter, otherwise, it returns the Option directly. 15 Returns the option value if the option contains a value, otherwise returns null. 8.34.1. Example #
object Test { def main(args: Array[String]) { val sites = Map("runoob" -> "www.runoob.com", "google" -> "www.google.com") println("sites.get( \\"runoob\\" ) : " + sites.get( "runoob" )) // Some(www.runoob.com) println("sites.get( \\"baidu\\" ) : " + sites.get( "baidu" )) // None } }
$ scalac Test.scala $ scala Test sites.get( "runoob" ) : Some(www.runoob.com) sites.get( "baidu" ) : None
8.34.2. Example #
object Test { def main(args: Array[String]) { val sites = Map("runoob" -> "www.runoob.com", "google" -> "www.google.com") println("show(sites.get( \\"runoob\\")) : " + show(sites.get( "runoob")) ) println("show(sites.get( \\"baidu\\")) : " + show(sites.get( "baidu")) ) } def show(x: Option[String]) = x match { case Some(s) => s case None => "?" } }
$ scalac Test.scala $ scala Test show(sites.get( "runoob")) : www.runoob.com show(sites.get( "baidu")) : ?
getOrElse() method # getOrElse() method to get the elements that exist in the tuple or use their default values, as an example: 8.34.3. 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) ) } }
$ scalac Test.scala $ scala Test a.getOrElse(0): 5 b.getOrElse(10): 10
isEmpty() method # isEmpty() method to detect whether the element in the tuple is None , the example is as follows: 8.34.4. 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 ) } }
$ scalac Test.scala $ scala Test a.isEmpty: false b.isEmpty: true
Common methods of Scala Option #
Option common methods: def get: A def isEmpty: Boolean def productArity: Int def productElement(n: Int): Any def exists(p: (A) => Boolean): Boolean def filter(p: (A) => Boolean): Option[A] def filterNot(p: (A) => Boolean): Option[A] def flatMap[B](f: (A) => Option[B]): Option[B] def foreach[U](f: (A) => U): Unit def getOrElse[B >: A](default: => B): B def isDefined: Boolean def iterator: Iterator[A] def map[B](f: (A) => B): Option[B] def orElse[B >: A](alternative: => Option[B]): Option[B] def orNull