How do I convert a scala.collection.Iterator
containing thousands of objects to a scala.collection.immutable.Vector
?
I do not believe that I can use _*
because of the number of items.
You can
Vector() ++ myIterator
which gives the correct thing with the correct type. For very small vectors and iterators, in high-performance loops, you may instead wish to
val b = Vector.newBuilder[WhateverType]
while (myIterator.hasNext) { b += myIterator.next }
b.result
which does the minimum work necessary (as far as I know) to create a vector. toIndexedSeq
does essentially this, but returns a more generic type (so you're not actually guaranteed a Vector
, even if it does return a Vector
now.)
You can use toIndexedSeq
. It doesn't statically return a Vector
, but it actually is one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With