None of the above answers hit the nail on the head.
This is a good reason Iterator is returning. If you are lazy, it takes pressure from the heap, and the line representing each line can be garbage collected as soon as you finish with it. In the case of large files, this may be important to throw an OutOfMemoryException.
Ideally, you will work directly with the iterator, and not force it into a strict collection type.
Using grouped , then, as per om-nom-nom:
for (linesSub <- lines grouped 10) { //do something with linesSub }
And if you want to keep the println counter, write to the index:
for ( (linesSub, batchIdx) <- (lines grouped 10).zipWithIndex ) { println("batch " + batchIdx) //do something with linesSub }
If you really need the amount, call getLines twice. Once for counting and a second time for actual line processing.
Kevin wright
source share