Java iterator over an empty collection of a parameterized type -
In Java, I have to return an Iterator to my method. My data comes from another object which can usually give me an Iterator, so I can return it, but in some circumstances the underlying data is zero. For continuity, I want to return a "empty" iterator in that case, so that my callers do not have to test for zero.
I have to write something like this:
Public Iterator & lt; Foo & gt; Iterator () {if (underlying data! = Null) {The underlying data. Director (); // works} and {return collection .emptyList (). Iterator (); // compiler error}} but Java compiler returning You can get an empty list of FUs through the following syntax: < Pre> Iterator & lt; Object & gt; Instead of Iterator & lt; Foo & gt; Complaints about . Casting (iterator return collection. & Lt; Foo & gt; Empty list (). Iterator ();
Comments
Post a Comment