Scala Option

Scala Option

Scala中的 Option 是指一个单一或无元素的载体,用于指定的类型。当一个方法返回一个甚至可以是空的值时,就会使用Option,也就是说,定义的方法会返回一个Option的实例,而不是返回一个单一的对象或空。

重要的一点是:

  • 这里返回的Option的实例可以是Scala中Some类或None类的实例,其中Some和None是Option类的子类。
  • 当获得一个给定的键的值时,会生成Some类。
  • 当一个给定的键的值没有被获得,那么None类将被生成。

例子:

// Scala program for Option
  
// Creating object
object option
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating a Map
        val name = Map("Nidhi" -> "author", 
                        "Geeta" -> "coder")
  
        // Accessing keys of the map
        val x = name.get("Nidhi")
        val y = name.get("Rahul")
  
        // Displays Some if the key is
        // found else None 
        println(x)
        println(y)
    }
}

输出。

Some(author)
None

在这里,Nidhi的键值被找到了,所以Some被返回,但是Rahul的键值没有被找到,所以None被返回。

获取可选值的不同方法

  • 使用模式匹配:

示例 :

// Scala program for Option
// with Pattern matching
  
// Creating object
object pattern
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating a Map
        val name = Map("Nidhi" -> "author", 
                        "Geeta" -> "coder")
  
        //Accessing keys of the map
        println(patrn(name.get("Nidhi")))
        println(patrn(name.get("Rahul")))
    }
  
    // Using Option with Pattern 
    // matching
    def patrn(z: Option[String]) = z match 
    {
  
        // for 'Some' class the key for
        // the given value is displayed
        case Some(s) => (s)
  
        // for 'None' class the below string 
        // is displayed
        case None => ("key not found")
    }
}

输出:

author
key not found

这里,我们在Scala中使用了Option与模式匹配。

  • getOrElse() 方法:

这个方法的作用是当它存在时返回一个值,当它不存在时返回一个默认值。在这里,对于Some类,将返回一个值,对于None类,将返回一个默认值。

示例:

// Scala program of using
// getOrElse method
  
// Creating object
object get
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Using Some class
        val some:Option[Int] = Some(15)
  
        // Using None class
        val none:Option[Int] = None 
  
        // Applying getOrElse method
        val x = some.getOrElse(0)
        val y = none.getOrElse(17)
  
        // Displays the key in the 
        // class Some
        println(x)
  
        // Displays default value 
        println(y)
    }
}

输出:

15
17

这里,分配给None的默认值是17,所以,它被返回给None类。

  • isEmpty() 方法:

这个方法被用来检查期权是否有价值。

示例:

// Scala program of using
// isEmpty method
  
// Creating object
object check
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Using Some class
        val some:Option[Int] = Some(20)
  
        // Using None class
        val none:Option[Int] = None 
  
        // Applying isEmpty method
        val x = some.isEmpty
        val y = none.isEmpty
  
        // Displays true if there
        // is a value else false
        println(x)
        println(y)
    }
}

输出:

false
true

在这里,isEmpty对Some类返回false,因为它不是空的,但对None返回true,因为它是空的。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程