8.34. Scala Option

发布时间 : 2025-10-25 13:35:12 UTC      

Page Views: 9 views

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:

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 } } 

Execute the above code, and the output is as follows:

$ scalac Test.scala $ scala Test sites.get( "runoob" ) : Some(www.runoob.com) sites.get( "baidu" ) : None 

You can also output matching values through pattern matching. Examples are as follows:

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 => "?" } } 

Execute the above code, and the output is as follows:

$ scalac Test.scala $ scala Test show(sites.get( "runoob")) : www.runoob.com show(sites.get( "baidu")) : ? 

getOrElse() method #

You can use 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) ) } } 

Execute the above code, and the output is as follows:

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

isEmpty() method #

You can use 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 ) } } 

Execute the above code, and the output is as follows:

$ scalac Test.scala $ scala Test a.isEmpty: false b.isEmpty: true 

Common methods of Scala Option #

The following table lists the Scala Option common methods:

Serial number

Method and description

1

def get: A

Get optional value

2

def isEmpty: Boolean

Check whether the optional type value is None. If so, return true, otherwisereturn false.

3

def productArity: Int

Returns the number of elements, A (xroom1,…, xroomk), returns k

4

def productElement(n: Int): Any

Gets the specified option, starting with 0. That is, A (x, n < k), returns x _ (n < 1), 0 < n < k.

5

def exists(p: (A) => Boolean): Boolean

Returns false if the element specified in the condition in the option existsand does not return true for None.

6

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

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

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

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

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

If the option contains a value, it is passed to the function f for processing and returned, otherwise it returns 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

If the option contains a value, returns the option value, otherwise returns the default value set.

11

def isDefined: Boolean

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

12

def iterator: Iterator[A]

If the option contains a value, iterate over the optional value. Returns an empty iterator if the optional value is empty.

13

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

If the option contains a value, return the Some processed by the function f,otherwise return None

14

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

If an Option is None, the orElse method returns the value of the name parameter, otherwise, it returns the Option directly.

15

def orNull

Returns the option value if the option contains a value, otherwise returns null.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。