No, this is not a mistake. This is normal behavior.
Iterators are mutable things. You can think of them as pointers. Each time you request an iterator to give you the next element, it indicates that it will move one position further.
When you ask him to give you size, he will cross each element in the sequence that he points to, moving each position one time to the right. When it has no more elements to move iterator.hasNext == false , it will return the size. But by then he will have exhausted all the elements. When a new size call is made, the iterator is already at the end, so it will immediately return 0.
To better understand what is happening, you can do this:
val it = Iterator(1, 2, 3, 4) //it: >1 2 3 4 it.next() //ask for the next element //it: 1 >2 3 4 it.next() //it: 1 2 >3 4 println(it.size) // prints 2 //it: 1 2 3 4 > println(it.size) // prints 0
source share