Creating an Iterator for a streaming JDBC ResultSet in Java
The Java Iterator interface requires one implements a hasNext method, to determine if the current item is the last to be iterated over, or not. The MySQL driver's implementation of the JDBC ResultSet object, if one uses streaming mode throws an exception from its isLast method. (Streaming mode prevents the JVM from running out of memory, which it would do if it tried to fetch all the results at once.)
Therefore I've developed an Iterator class based on such a ResultSet whose "next" method actually pre-fetches the row after the current one. The Iterator's "hasNext" method therefore just returns if the row was created or not. And the "next" method returns the pre-fetched one, and fetches the next one.
And in order to make this code reusable, it's an abstract superclass, and you can implement a method in a concrete subclass which converts the row into an object of your choosing. And thus the concrete subclass will provide an implementation of
Iterator<T> for your T.