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 Iterator & lt; Object & gt; Instead of Iterator & lt; Foo & gt; Complaints about . Casting (iterator ) does not work either.

You can get an empty list of FUs through the following syntax: < Pre> return collection. & Lt; Foo & gt; Empty list (). Iterator ();


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -