scalaiteratorscala-2.13

What is the proper way to convert Iterator to Iterable in Scala 2.13?


When upgrading the scala version of an existing code base the build warns that the .toIterable method on an Iterator is deprecated.

How can an Iterator be converted to an Iterable in Scala 2.13?

Intellij recommends

use .iterator.to(Iterable)

However, the .iterator seems redundant.

Thank you in advance for your consideration and response.


Solution

  • Generally,

       val v: Iterator[Int] = ???
       val vv = v.to(Iterable)
    

    but if you need more than one iteration

       val v: Iterator[Int] = ???
       val vv: Iterable[Int] = v.to(List)