Scala Option
The Scala Option type is used to represent a value that is optional (either having a value or no value).
Option[T] is a container for an optional value of type T: if the value exists, Option[T] is a Some[T]; if not, Option[T] is the object None.
Next let's look at a piece of code:
// Although Scala allows you not to declare a variable's type, for clarity I still
// declared it explicitly
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 code above, myMap is a hash map whose Key type is String and Value type is String; the difference is that its get() returns a type called Option[String].
Scala uses Option[String] to tell you: "I will try to return a String, but there may be no String for you."
myMap does not contain the data for key2, so the get() method returns None.
Option has two subtypes, Some and None. When it returns Some, it means the function successfully gave you a String, which you can retrieve via get(); if it returns None, it means there is no String for you.
Another example:
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
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
sites.get( "runoob" ) : Some(www.runoob.com)
sites.get( "baidu" ) : None
You can also use pattern matching to output the matched value. Example as follows:
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
=>
"?"
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
show(sites.get( "runoob")) : www.runoob.com
show(sites.get( "baidu")) : ?
The getOrElse() Method
You can use the getOrElse() method to get an element that exists in the tuple or use its default value. Example as follows:
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
)
)
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
a.getOrElse(0): 5
b.getOrElse(10): 10
The isEmpty() Method
You can use the isEmpty() method to check whether an element in the tuple is None. Example as follows:
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
)
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
a.isEmpty: false
b.isEmpty: true
Scala Option Common Methods
The following table lists the commonly used methods of Scala Option:
| No. | Method and description |
|---|---|
| 1 |
def get: A Gets the optional value |
| 2 |
def isEmpty: Boolean Tests whether the optional value is None; returns true if so, 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 optional item, starting at 0. That is, A(x_1, ..., x_k) returns x_(n+1), 0 < n < k. |
| 5 |
def exists(p: (A) => Boolean): Boolean Returns true if an element satisfying the specified condition exists in the option and is not None, false otherwise. |
| 6 |
def filter(p: (A) => Boolean): Option[A] If the option contains a value and the condition function passed to filter returns true, filter returns a Some instance; otherwise it returns None. |
| 7 |
def filterNot(p: (A) => Boolean): Option[A] If the option contains a value and the condition function passed to filter returns false, filter returns a Some instance; otherwise it returns None. |
| 8 |
def flatMap[B](f: (A) => Option[B]): Option[B] If the option contains a value, passes it to function f and returns the result; otherwise returns None |
| 9 |
def foreach[U](f: (A) => U): Unit If the option contains a value, passes each value to function f; otherwise does nothing. |
| 10 |
def getOrElse[B >: A](default: => B): B If the option contains a value, returns it; otherwise returns the specified default value. |
| 11 |
def isDefined: Boolean Returns true if the optional value is an instance of Some, false otherwise. |
| 12 |
def iterator: Iterator[A] If the option contains a value, iterates over the optional value; if it is empty, returns an empty iterator. |
| 13 |
def map[B](f: (A) => B): Option[B] If the option contains a value, returns a Some processed by function f; otherwise returns None |
| 14 |
def orElse[B >: A](alternative: => Option[B]): Option[B] If an Option is None, the orElse method returns the value of the by-name parameter; otherwise it returns the Option itself. |
| 15 |
def orNull If the option contains a value, returns it; otherwise returns null. |
Scala Collections