In Java, I need to return an Iterator from my method. My data comes from another object which usually can give me an iterator so I can just return that, but in some circums
I'd go more along the lines of
public Iterator iterator() {
if (underlyingData == null)
return Collections. emptyList().iterator();
return underlyingData.iterator();
}
Just, handle the special case and return, then handle the normal case. But my main point is that you can avoid the assignment with
Collections. emptyList().iterator();