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