I want to implement a method like this:
public Iterator<File> getFiles(String root) {
// return an Iterator looping through all files in root and all files in sub-directories of roots (recursively)
}
In C#, this can easily be implemented with the yield return
keyword. In Java, I suspect I have to end up writing a lot of complicated code to get it done. Is there any good solution to this problem?
Edit: I want the returned Iterator to be "lazy", i.e. only return a new File when next()
is invoked. (That is the behavior C#'s yield return
offers.)