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