Scala Iterator toIterable() method with example
Last Updated :
03 Jun, 2019
The toIterable() belongs to the concrete value members of the Class AbstractIterator. It is defined in the classes TraversableOnce and GenTraversableOnce. It converts the stated traversable or iterator to an iterable collection. It won’t end for infinite-sized collections.
Method Definition : def toIterable: Iterable[A]
Return Type : It returns an Iterable containing all elements of the stated traversable or iterator.
Example :
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 3 , 2 , 5 , 9 )
val result = iter.toIterable
println(result)
}
}
|
Here, a Stream is returned from the stated iterator.
Example :
object GfG
{
def main(args : Array[String])
{
val iter = Iterator()
val result = iter.toIterable
println(result)
}
}
|
Here, an empty Stream is returned from an empty-iterator.
Share your thoughts in the comments
Please Login to comment...