Scala SortedSet exists()方法及示例
exists() 方法是用来测试一个谓词对排序集的某些元素是否成立。
方法定义: def exists(p: (A) => Boolean):Boolean
返回类型。如果所述谓词对排序集的某些元素成立,则返回真,否则返回假。
例子 #1:
// Scala program of exists()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val s1 = SortedSet(1, 2, 3, 4, 5)
// Applying exists method
val result = s1.exists(y => {y % 3 == 0})
// Displays output
println(result)
}
}
输出。
true
例子#2。
// Scala program of exists()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val s1 = SortedSet(1, 2, 3, 4, 5)
// Applying exists method
val result = s1.exists(y => {y % 7 == 0})
// Displays output
println(result)
}
}
输出。
false
极客教程