Scala Option (Option)
Scala Option (Option) type used to represent a value is optional (for value or no value).
Option [T] is a type of container optional value T: If the value exists, Option [T] is a Some [T], if you do not exist, Option [T] is the object None.
Next we look at a piece of code:
// 虽然 Scala 可以不定义变量的类型,不过为了清楚些,我还是 // 把他显示的定义上了 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 one is a Key type is String, String type Value is a hash map, but not the same as his get () returns a named Option [String] category.
Scala using the Option [String] to tell you: "I will try to return a String, but it may not give you the String."
myMap was not key2 this data, get () method returns None.
Option two subcategories, one Some one is None, when he return Some of the time, on behalf of this function successfully gave you a String, and you can () function to get this through the String get, If he returns None, it represents no string can give you.
Another example:
object Test { def main(args: Array[String]) { val sites = Map("w3big" -> "www.w3big.com", "google" -> "www.google.com") println("sites.get( \"w3big\" ) : " + sites.get( "w3big" )) // Some(www.w3big.com) println("sites.get( \"baidu\" ) : " + sites.get( "baidu" )) // None } }
Implementation of the above code, the output is:
$ scalac Test.scala $ scala Test sites.get( "w3big" ) : Some(www.w3big.com) sites.get( "baidu" ) : None
You can also output to match the value by pattern matching. Examples are as follows:
object Test { def main(args: Array[String]) { val sites = Map("w3big" -> "www.w3big.com", "google" -> "www.google.com") println("show(sites.get( \"w3big\")) : " + show(sites.get( "w3big")) ) println("show(sites.get( \"baidu\")) : " + show(sites.get( "baidu")) ) } def show(x: Option[String]) = x match { case Some(s) => s case None => "?" } }
Implementation of the above code, the output is:
$ scalac Test.scala $ scala Test show(sites.get( "w3big")) : www.w3big.com show(sites.get( "baidu")) : ?
getOrElse () method
You can use getOrElse () method to get the tuple that exist or use the default values, examples are as follows:
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) ) } }
Implementation of 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 detect whether the tuple element to None, examples are as follows:
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 ) } }
Implementation of the above code, the output is:
$ scalac Test.scala $ scala Test a.isEmpty: false b.isEmpty: true
Scala Option common method
The following table lists the Scala Option commonly used methods:
No. | Method and Description |
---|---|
1 | def get: A Get optional value |
2 | def isEmpty: Boolean Optional type value detecting whether to None, this is the case returns true, false otherwise |
3 | def productArity: Int Returns the number of elements, A (x_1, ..., x_k), returns k |
4 | def productElement (n: Int): Any Gets the specified options, starting with zero. Namely A (x_1, ..., x_k), return x_ (n + 1), 0 <n <k. |
5 | def exists (p: (A) => Boolean): Boolean If the option whether the conditions specified element exists and is not None returns true, otherwise it returns false. |
6 | def filter (p: (A) => Boolean): Option [A] Con If the option contains the value, and passed to filter returns true, filter returns Some instances. Otherwise, the return value is None. |
7 | def filterNot (p: (A) => Boolean): Option [A] Con If the option contains the value, and passed to filter returns false, filter returns Some instances. Otherwise, the return value is None. |
8 | def flatMap [B] (f: (A) => Option [B]): Option [B] If the option has a value, then passed to the function f returns after treatment, otherwise it returns None |
9 | def foreach [U] (f: (A) => U): Unit If the option contains a value, then each value passed to the function f, or not treated. |
10 | def getOrElse [B>: A] (default: => B): B If the option has a value, the option value is returned, otherwise the default values set. |
11 | def isDefined: Boolean If the optional value is an instance Some returns true, otherwise returns false. |
12 | def iterator: Iterator [A] If the option has a value, an optional value iteration. If the optional value is empty empty iterator is returned. |
13 | def map [B] (f: (A) => B): Option [B] If the option contains the value, returned by the function f Some post-processing, otherwise return None |
14 | def orElse [B>: A] (alternative: => Option [B]): Option [B] If a Option is None, orElse method returns the value of the parameter by name, otherwise, it returns directly to the Option. |
15 | def orNull If the option contains the option values return value, otherwise return null. |