Why iterator doesn't have any reset method?

前端 未结 5 1308
星月不相逢
星月不相逢 2020-12-29 19:52

Why? And what the best way to move iterator items pointer to the first position?

5条回答
  •  难免孤独
    2020-12-29 20:36

    Tip: create your iterator variable as a function instead, then you can consume it as many times are you want. This only works if the underlying logic is repeatable.

    Example in Scala (Java similar but I don't have a Java REPL handy)

    def i = (1 to 100) iterator   // i is our iterator
    i.grouped(50) foreach println // prints two groups
    i.grouped(50) foreach println // prints same two groups again
    

提交回复
热议问题