Scala Iterator exists() 方法与示例
exists()方法属于Abstract Iterator类的具体值成员。它在类IterableOnceOps中定义。它测试使用的谓词是否适用于所述集合的至少一个元素。它不会因无限大小的集合而终止。
Method Definition : def exists(p: (A) => Boolean): Boolean
Where, p is the predicate.
Return Type : It returns true if the given predicate p is satisfied by at least one element of the stated collection, else it returns false.
示例 #1:
// Scala program of exists()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating an Iterator
val iter = Iterator(7, 3, 4, 11, 13)
// Applying exists method
val result = iter.exists(x => {x % 2 == 0})
// Displays output
println(result)
}
}
输出:
true
示例 #2:
// Scala program of exists()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating an Iterator
val iter = Iterator(7, 3, 5, 11, 13)
// Applying exists method
val result = iter.exists(x => {x % 2 == 0})
// Displays output
println(result)
}
}
输出:
false