title: Limit a ListIterator to the first N elements (optimized)
body: <p>What is a simple and fast way to get an iterator that returns at most N elements from the start of a <code>List</code>?</p>
  
  <p>The simplest versions I could come up with are:</p>
  
  <p>#1:</p>
  
  <pre><code>import com.google.common.collect.Iterators;
  
  // ...
  
  public static &lt;E&gt; Iterator&lt;E&gt; lengthLimitedIterator(Iterable&lt;E&gt; source, int maxLen) {
      return Iterators.partition(source.iterator(), maxLen).next().iterator();
  }
  </code></pre>
  
  <p>#2:</p>
  
  <pre><code>public static &lt;E&gt; Iterator&lt;E&gt; lengthLimitedIterator(List&lt;E&gt; source, int maxLen) {
      return source.subList(0, Math.min(source.size(), maxLen)).iterator();
  }
  </code></pre>
  
  <p>Unfortunately both versions create a temporary <code>List</code> which significantly affects performance as I am calling this method millions of times in a tight loop.</p>
  
  <p>Are there any other library functions I could use for this?</p>
  
  <hr>
  
  <p>Note: I cannot avoid iterating over the list as I am passing it to a method which takes an iterator as its argument and I cannot modify that class.</p>