title: How to convert an Iterator to a Stream?
body: <p>I am looking for a concise way to convert an <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html" rel="noreferrer"><code>Iterator</code></a> to a <a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html" rel="noreferrer"><code>Stream</code></a> or more specifically to "view" the iterator as a stream.</p>
  
  <p>For performance reason, I would like to avoid a copy of the iterator in a new list:</p>
  
  <pre><code>Iterator&lt;String&gt; sourceIterator = Arrays.asList("A", "B", "C").iterator();
  Collection&lt;String&gt; copyList = new ArrayList&lt;String&gt;();
  sourceIterator.forEachRemaining(copyList::add);
  Stream&lt;String&gt; targetStream = copyList.stream();
  </code></pre>
  
  <hr>
  
  <p>Based on the some suggestions in the comments, I have also tried to use <a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#generate-java.util.function.Supplier-" rel="noreferrer"><code>Stream.generate</code></a>:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      Iterator&lt;String&gt; sourceIterator = Arrays.asList("A", "B", "C").iterator();
      Stream&lt;String&gt; targetStream = Stream.generate(sourceIterator::next);
      targetStream.forEach(System.out::println);
  }
  </code></pre>
  
  <p>However, I get a <code>NoSuchElementException</code> (since there is no invocation of <code>hasNext</code>)</p>
  
  <pre class="lang-none prettyprint-override"><code>Exception in thread "main" java.util.NoSuchElementException
      at java.util.AbstractList$Itr.next(AbstractList.java:364)
      at Main$$Lambda$1/1175962212.get(Unknown Source)
      at java.util.stream.StreamSpliterators$InfiniteSupplyingSpliterator$OfRef.tryAdvance(StreamSpliterators.java:1351)
      at java.util.Spliterator.forEachRemaining(Spliterator.java:326)
      at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
      at Main.main(Main.java:20)
  </code></pre>
  
  <p>I have looked at <a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/StreamSupport.html" rel="noreferrer"><code>StreamSupport</code></a> and <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html" rel="noreferrer"><code>Collections</code></a> but I didn't find anything.</p>