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