title: Is an "infinite" iterator bad design?
body: <p>Is it generally considered bad practice to provide <code>Iterator</code> implementations that are "infinite"; i.e. where calls to <code>hasNext()</code> always(*) return true?</p>
  
  <p>Typically I'd say "yes" because the calling code could behave erratically, but in the below implementation <code>hasNext()</code> will return true unless the caller removes all elements from the List that the iterator was initialised with; i.e. <strong>there is a termination condition</strong>.  Do you think this is a legitimate use of <code>Iterator</code>?  It doesn't seem to violate the contract although I suppose one could argue it's unintuitive.</p>
  
  <pre><code>public class CyclicIterator&lt;T&gt; implements Iterator&lt;T&gt; {
    private final List&lt;T&gt; l;
    private Iterator&lt;T&gt; it;
  
    public CyclicIterator&lt;T&gt;(List&lt;T&gt; l) {
      this.l = l;
      this.it = l.iterator();
    }
  
    public boolean hasNext() {
      return !l.isEmpty();
    }
  
    public T next() {
      T ret;
  
      if (!hasNext()) {
        throw new NoSuchElementException();
      } else if (it.hasNext()) {
        ret = it.next();
      } else {
        it = l.iterator();
        ret = it.next();
      }
  
      return ret;
    }
  
    public void remove() {
      it.remove();
    }
  }
  </code></pre>
  
  <p><strong>(Pedantic) EDIT</strong></p>
  
  <p>Some people have commented how an <code>Iterator</code> could be used to generate values from an unbounded sequence such as the Fibonacci sequence.  However, the Java <code>Iterator</code> documentation states that an Iterator is:</p>
  
  <blockquote>
    <p>An iterator over a collection.</p>
  </blockquote>
  
  <p>Now you could argue that the Fibonacci sequence is an infinite collection but in Java I would equate collection with the <code>java.util.Collection</code> interface, which offers methods such as <code>size()</code> implying that a collection must be bounded.  Therefore, is it legitimate to use <code>Iterator</code> as a generator of values from an unbounded sequence?</p>