toIterator
defined in TraversableLike
as
def toIterator: Iterator[A] = toStream.iterator
therefore, it creates a Stream
in the background, which stores all the elements in memory during the iteration.
( Edit ): I think the stream structure is not really a problem. However, toStream
itself calls toBuffer
, which in turn copies each value.)
iterator
, on the other hand, is defined in IndexedSeqLike
, which uses a specialized structure that does not contain any elements in memory.
source share