title: Java iterator over an empty collection of a parameterized type
body: <p>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 circumstances the underlying data is null.  For consistency, I want to return an "empty" iterator in that case so my callers don't have to test for null.</p>
  
  <p>I wanted to write something like:</p>
  
  <pre><code>public Iterator&lt;Foo&gt; iterator() {
     if (underlyingData != null) {
        return underlyingData.iterator();  // works
     } else {
        return Collections.emptyList().iterator();  // compiler error
     }
  }
  </code></pre>
  
  <p>But the Java compiler complains about the returning <code>Iterator&lt;Object&gt;</code> instead of <code>Iterator&lt;Foo&gt;</code>.  Casting to <code>(Iterator&lt;Foo&gt;)</code> doesn't work either.</p>