Tag: iterators
-
Java: Iterators are not Iterable
This is something I stumbled across multiple times now in Java: Iterators are not Iterable. Java 1.5 introduced the foreach statement, which allows easy iteration over collections like this: for (MyClass item: myCollection) { doStuffWithItem(item); } For this to work class MyClass must implement the Iterable interface. This interface defines just one method that returns an Iterator: Iterator<T> iterator(); This works fine in many cases.…