[scala] Option type

Generally speaking, there is a keyword for each language to denote "none" of an object reference. In Java the one used is null.

Scala, on the other hand, incorporates a functional programming style, using the Option type when it is expected that a variable or function return value may not refer to any value.

The Option type is used to represent a value. (with or without value).

Option[T] is a container of an optional value of type T.

The Option class contains a subclass Some, when there is a value that can be referenced, that is, there is a value, you can use Some to contain this value. For example Some("Hadoop")

The Option class also contains a subclass of None, which has no value. Both Some and None are its subclasses, they are both final classes, so there can be no derived subclasses.

An Option instance is an instance of a Some or None object.

First let's look at this example

scala> val book = Map("hadoop" -> 5, "spark" -> 10, "hbase" -> 7);//We define a Map in the scala shell
book: scala.collection.immutable.Map[String,Int] = Map(hadoop -> 5, spark -> 10, hbase -> 7)

scala> book.get("hadoop")//Then we get the map whose key is hadoop from the Map
res0: Option[Int] = Some(5) //The type of the value we get is Option[Int], the value is contained in Some and returned, reading is actually an operation on Some
scala> book.get("hive")//We then get the map whose key is hive from the Map res1: Option[Int] = None//This value does not exist, so the returned value is a None object

When we want to change the None value to more meaningful data, we can call the getOrElse method, and getOrElse can only replace the None value.

scala> book.get("hive").getOrElse("No such book")
res2: Any = No such book

How do we define the use of the Option type?

scala> val a : Option[Int] = Some(5) //Define an Option instance a with a value of 5
a: Option[Int] = Some(5)

scala> val b : Option[Int] = None //Define a valueless Option instance b
b: Option[Int] = None

scala> a.getOrElse(0) //Calling getOrElse on a valued Option instance cannot replace the value
res5: Int = 5

scala> b.getOrElse(0) //getOrElse can only replace the None value
res6: Int = 0

Option is actually a container

We can think of it as a set, but this set either contains only one element contained in Some, or there is no element showing None

In fact, it is not a real collection class, because it does not inherit Traversable or Iterable.

But it does have all the operations of Iterable, which means that you can use Option as a collection.

The Option class can use most of the collection operations. For example map, foreach, filter, etc.

scala> book.get("hive").foreach(println) //When foreach traverses to None, do nothing and do not execute println

scala> book.get("hadoop").foreach(println)
5

some common methods

1

def get: A

get optional value

2

def isEmpty: Boolean

Check if the optional type value is None, return true if it is, otherwise return false

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, starting with 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 the element with the specified condition in the optional exists and is not None, false otherwise.

6

def filter(p: (A) => Boolean): Option[A]

If the option contains a value and the conditional function passed to filter returns true, filter returns an instance of Some. Otherwise, the return value is None.

7

def filterNot(p: (A) => Boolean): Option[A]

If the option contains a value and the conditional function passed to filter returns false, filter returns an instance of Some. Otherwise, the return value is None.

8

def flatMap[B](f: (A) => Option[B]): Option[B]

If the option contains a value, it will be passed to the function f and returned after processing, otherwise it will return None

9

def foreach[U](f: (A) => U): Unit

If the option contains values, each value is passed to the function f, otherwise it is not processed.

10

def getOrElse[B >: A](default: => B): B

Returns the option value if the option contains a value, otherwise returns the set default value.

11

def isDefined: Boolean

Returns true if the optional value is an instance of Some, otherwise returns false.

12

def iterator: Iterator[A]

If options contain values, iterate over the optional values. Returns an empty iterator if the optional value is empty.

13

def map[B](f: (A) => B): Option[B]

如果选项包含有值, 返回由函数 f 处理后的 Some,否则返回 None

14

def orElse[B >: A](alternative: => Option[B]): Option[B]

如果一个 Option 是 None , orElse 方法会返回传名参数的值,否则,就直接返回这个 Option。

15

def orNull

如果选项包含有值返回选项值,否则返回 null。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324845958&siteId=291194637