Scala选项Option用法介绍和解释

2021年3月17日18:32:17 发表评论 678 次浏览

Scala中的选项被称为指定类型的单个或没有元素的载体。当方法返回的值甚至可以为null时, 则使用Option, 即, 所定义的方法返回Option的实例, 而不是返回单个对象或null。

重要事项:

  • 在此返回的Option实例可以是一些类或None在Scala上课一些和None是...的孩子选项类。
  • 当获得给定键的值时一些类已生成。
  • 如果未获得给定密钥的值, 则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

在这里, 价值的关键尼地被发现是这样, 一些为此返回, 但值的键拉胡尔找不到, 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

在这里, 我们将Option与模式匹配在斯卡拉。

getOrElse()方法:

此方法用于返回值(如果存在)或默认值(如果不存在)。在这里

一些

类返回一个值, 并且

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

这里, 是空的传回false一些类, 因为它是非空的, 但对于None为空。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: