Scala Iterator find() method with example
Last Updated :
30 Jun, 2019
The find() method belong to the concrete value members of the class AbstractIterator. It is defined in the class IterableOnceOps. It finds the first element of the stated collection which satisfies the given predicate. It would not terminate for the infinite sized collections.
Method Definition : def find(p: (A) => Boolean): Option[A]
Return Type :It returns an Option value containing the first element of the stated collection that satisfies the used predicate else returns None if none exists.
Example #1:
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 2 , 4 , 5 , 1 , 13 )
val result = iter.find(x => {x % 2 == 0 })
println(result)
}
}
|
Example #2:
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 3 , 6 , 15 , 12 , 21 )
val result = iter.find(x => {x % 3 ! = 0 })
println(result)
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...