Scala Iterator forall() 方法与示例
forall()方法属于 Class AbstractIterator的具体值成员。它在类IterableOnceOps中定义。它测试使用的谓词是否适用于所述集合的所有元素。对于无限大小的集合,它可能不会终止。
Method Definition : def forall(p: (A) => Boolean): Boolean
Return Type : It returns true if the stated collection is empty or if the given predicate p holds for all elements of this collection, else it returns false.
示例 #1:
// Scala program of forall()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating an Iterator
val iter = Iterator(3, 6, 15, 12, 21)
// Applying forall method
val result= iter.forall(x => {x % 3 == 0})
// Displays output
println(result)
}
}
输出:
true
示例 #2:
// Scala program of forall()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating an Iterator
val iter = Iterator(3, 6, 15, 19, 21)
// Applying forall method
val result= iter.forall(x => {x % 3 == 0})
// Displays output
println(result)
}
}
输出:
false