Scala Iterator count() method with example
Last Updated :
30 Jun, 2019
The count() method belongs to the concrete value members of the class Abstract Iterator. It is defined in the class IterableOnceOps. It counts the number of elements in the stated collection which satisfy the given predicate.
Method Definition : def count(p: (A) => Boolean): Int
Where, p is the predicate used.
Return Type :It returns the number of elements satisfying the predicate p.
Example #1:
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 5 , 6 , 8 )
val result = iter.count(x => {x % 3 ! = 0 })
println(result)
}
}
|
Here, only two elements satisfies the stated predicate so, two is returned.
Example #2:
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 4 , 6 , 10 , 11 , 13 )
val result = iter.count(x => {x % 2 == 0 })
println(result)
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...