Scala Iterator hasNext() method with example
Last Updated :
30 Jun, 2019
The hasNext() method belongs to the Abstract Value Members of the Class AbstractIterator. It is defined in the class Iterator. It checks whether there is a next element available or not.
Method Definition : abstract def hasNext: Boolean
Return Type :It returns true if there is a next element, else it returns false.
Example #1:
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 2 , 3 , 4 , 5 )
val result = iter.hasNext
println(result)
}
}
|
Therefore, true is returned as there is a next element.
Example #2:
object GfG
{
def main(args : Array[String])
{
val iter = Iterator()
val result = iter.hasNext
println(result)
}
}
|
Therefore, false is returned as there is no next element.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...