Scala Iterator indexWhere() method with example
Last Updated :
28 May, 2019
The indexWhere() method belongs to the concrete value members of iterator class in Scala. This method will find the position of the first value of the iterator that satisfies the stated predicate.
Example :
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 3 , 1 , 4 , 9 , 6 )
val result = iter.indexWhere(x => {x % 2 == 0 })
println(result)
}
}
|
Here, the given predicate is satisfied by the value of the iterator at the third position i.e, index 2 so, two is returned.
Example :
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 3 , 1 , 7 , 9 , 15 )
val result = iter.indexWhere(x => {x % 2 == 0 })
println(result)
}
}
|
Here, the stated predicate is not satisfied by any of the values of the iterator so, -1 is returned in such cases.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...