Scala Option调用的方法
Scala中的Option指的是一个指定类型的单一或无元素的载体。当一个方法返回一个甚至可以是空的值时,就会使用Option,即定义的方法返回一个Option的实例,而不是返回一个单一的对象或空值。
有几个方法我们可以调用Scala Option。
def get: A
这个方法被用来返回一个Option的值。
示例:
// Scala program of using
// get method
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Using Some class
val some:Option[Int] = Some(20)
// Applying get method
val x = some.get
// Displays the value
println(x)
}
}
输出:
20
这里,get方法不能应用于None类,因为它将显示一个异常。
def productArity: Int
这个方法用来返回Option的值的大小。
示例:
// Scala program of returning
// the size of the value
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Using Some class
val some:Option[Int] = Some(20)
// Applying productArity
// method
val x = some.productArity
// Displays the size of
// the Option's value
println(x)
}
}
输出:
“`scala 1
* `def productElement(n: Int): Any`
这个方法用来返回所述乘积的第n个元素,这里的索引从0开始。
示例:
```scala
// Scala program of returning
// the n-th element of the
// product
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Using Some class
val some:Option[Int] = Some(20)
// Applying productElement
// method
val x = some.productElement(0)
// Displays the element
println(x)
}
}
输出:
20
这里,”None”将显示一个例外。
def exists(p: (A) => Boolean): Boolean
当选项的值满足规定的条件时,该方法返回真,否则返回假。
示例:
// Scala program of the method
// 'exists'
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Using Some class
val some:Option[Int] = Some(30)
// Applying exists method
val x = some.exists(y => {y % 3 == 0})
// Displays true if the condition
// given is satisfied else false
println(x)
}
}
输出:
true
这里,所述的条件得到满足,所以返回true。
def filter(p: (A) => Boolean): Option[A]
如果所述条件得到满足,该方法被用来返回Option的值。
示例:
// Scala program of the method
// 'filter'
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Using Some class
val some:Option[Int] = Some(30)
// Applying filter method
val x = some.filter(y => {y % 3 == 0})
// Displays the value of the
// option if the predicate
// is satisfied
println(x)
}
}
输出:
Some(30)
这里,条件得到满足,所以返回Option值,如果不满足谓词,则返回None。
def filterNot(p: (A) => Boolean): Option[A]
如果所述条件没有得到满足,该方法将返回Option值。
示例:
// Scala program of the method
// 'filterNot'
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Using Some class
val some:Option[Int] = Some(30)
// Applying filterNot method
val x = some.filterNot(y => {y % 3 != 0})
// Displays the value of the
// option if the predicate
// is not satisfied
println(x)
}
}
输出:
Some(30)
这里,条件没有得到满足,所以返回Option值,如果谓词得到满足则返回None。
def isDefined: Boolean
如果选项是一个Some的实例,该方法返回真,如果选项是一个None的实例,则返回假。
示例:
// Scala program of the method
// 'isDefined'
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Using Some class
val some:Option[Int] = Some(30)
// using None class
val none:Option[Int] = None
// Applying isDefined method
val x = some.isDefined
val y = none.isDefined
// Displays true for Some
// and false for None
println(x)
println(y)
}
}
输出:
true
false
def iterator: Iterator[A]
该方法返回一个关于选项的迭代器。
示例:
// Scala program of the method
// 'iterator'
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Using Some class
val some:Option[Int] = Some(30)
// Applying iterator method
val x = some.iterator
// Displays an iterator
println(x)
}
}
输出:
non-empty iterator
def map[B](f: (A) => B): Option[B]
如果选项有一个值,该方法将返回地图中所述的函数值。
示例:
// Scala program of the method
// 'map'
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Using Some class
val some:Option[Int] = Some(30)
// Applying Map method
val x = some.map(y => {y + y})
// Displays the value returned
// by the function in map
println(x)
}
}
输出:
Some(60)
这些是对一个Option的调用方法,还有更多这样的方法。
-
def orElse[B >: A](alternative: => Option[B]): Option[B]
如果选项中包含一个值,这个方法将返回它。否则,该方法将评估替代值并返回替代值。 -
def orNull
如果选项不包含一个值,该方法将返回Null。
极客教程