Lazy evaluation of streams in Java: stream has already been operated upon or closed : <p>As per Java 8 API and <a href="https://stackoverflow.com/questions/21219667/stream-and-lazy-evaluation">this</a> , the streams are evaluated lazily. I have the following code snippet:</p>

<pre><code>List&lt;String&gt; l = Arrays.asList("d", "dw", "ew", "jj", "hh"); //Line 1
Stream&lt;String&gt; ls=l.stream();                                //Line 2
ls.map(a-&gt;a+"e");                                            //Line 3
ls.map(a-&gt;a+"de").foreach(System.out::println);              //Line 4
</code></pre>

<p>If streams are lazy, then, in my understanding, <em>ls.map</em> should return a new stream both the times i.e. line 3 and line 4 without really evaluating the lambda. When I run this piece of code, following is the output pointing error on Line 4:</p>

<pre><code>Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed 
</code></pre>

<p>When I repeat this exercise with Iterators in Scala which are also lazy. I receive no such errors:</p>

<pre><code>val a = List(1,2,3,4);
res1=a.iterator;    //Iterator[Int] = non-empty iterator
res2=res1.map(_+1); //Iterator[Int] = non-empty iterator
res3=res1.map(_+1); //Iterator[Int] = non-empty iterator
res3.foreach(println) //1,2,3,4
res2.foreach(println) //no output as the iterator has "traversedonce"
</code></pre>

<p>Regardless of my comparison with Scala, my basic focus is on the claimed laziness. Am I missing something when it comes to Java's Streams' implementation of laziness?</p>
