Scala ListSet find() 方法与示例
在 Scala ListSet 中,find() 方法用于查找满足给定谓词的列表集的第一个元素。
Method Definition: def find(p: (A) => Boolean): Option[A]
Return Type: It returns the first element of the listset which satisfies the given predicate.
示例 1:
// Scala program of find()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating ListSet
val m1 = ListSet(1, 2, 3)
// Applying find method
val result = m1.find(_>1)
// Displays output
println(result)
}
}
输出:
Some(3)
示例 2:
// Scala program of find()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating ListSet
val m1 = ListSet(1, 2, 3)
// Applying find method
val result = m1.find(_<1)
// Displays output
println(result)
}
}
输出:
None