Java8 stream with memoization
<p>How can I reuse in java8 (maybe a memoization process) values already computed through iteration over a stream?</p>

<p>If the stream is duplicated or supplied again it will be recomputed. In some cases it would be preferable to trade memory for that cpu time. Collecting everything from the beginning might not be a good idea since the stream is used to find the first item that satisfies a predicate.</p>

<pre><code>Stream&lt;Integer&gt; all = Stream.of(1,2,3,4,5, ...&lt;many other values&gt;... ).
      map(x-&gt;veryLongTimeToComputeFunction(x));
System.out.println("fast find of 2"+all.filter(x-&gt;x&gt;1).findFirst());

//both of these two lines generate a "java.lang.IllegalStateException: stream has already been operated upon or closed"
System.out.println("no find"+all.filter(x-&gt;x&gt;10).findFirst());
System.out.println("find again"+all.filter(x-&gt;x&gt;4).findFirst());
</code></pre>

<hr>

<p>The question is simillar to <a href="https://stackoverflow.com/questions/23860533/copy-a-stream-to-avoid-stream-has-already-been-operated-upon-or-closed-java-8">Copy a stream to avoid &quot;stream has already been operated upon or closed&quot; (java 8)</a></p>