I want to create an Iterator
in Java (i.e. support hasNext()
and next()
calls). However, the thing I'm iterating is generated recursively. I want to be able to create this Iterator
without having to generate the values beforehand and storing them. Instead, the next()
call should get the next value that the recursion outputs, and hasNext()
should check that the recursion is not yet done.
Is there a standard pattern for doing this?
Edit: I think a Python yield
in a recursive function is the functionality I'm looking for. Not sure how to do it in Java though.