The toList() method belongs to the concrete value members of the AbstractIterable class and is defined in the TraversableOnce and GenTraversableOnce classes. It converts a traversable or iterator to a list but it doesn’t terminates for infinite-sized collections.
-
Method Definition:
def toList: List[A]
- Return Type:
It returns a list from the stated traversable or an iterator.>/li>
Example :
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 8 , 9 , 10 , 11 )
val result = iter.toList
println(result)
}
}
|
Output:
List(8, 9, 10, 11)
Therefore, a List is generated from an iterator.
Example :
object GfG
{
def main(args : Array[String])
{
val iter = Iterator()
val result = iter.toList
println(result)
}
}
|
Therefore, an empty List is generated from an empty-iterator.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 May, 2019
Like Article
Save Article