Scala Iterator drop() method with example
Last Updated :
30 Jun, 2019
The drop() method belongs to the concrete value member of the class Abstract Iterator. It is defined in the classes Iterator and IterableOnceOps. It is utilized to move the iterator n places ahead and if n is greater than the iterator’s length, then it will throw an exception.
Method Definition : def drop(n: Int): Iterator[A]
Where, n is the number of elements to be dropped from the stated iterator.
Return Type : It returns all the elements of the stated iterator except the first n ones.
Example #1:
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 4 , 6 , 10 , 11 , 13 )
val x = iter.drop( 4 )
val result = x.next()
println(result)
}
}
|
Here, the first four elements are dropped and all the elements after that are returned.
Example #2:
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 2 , 3 , 4 )
val x = iter.drop( 1 )
val result = x.next()
println(result)
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...