Java 8: Despite the fact that you are avoiding the terminal, see "The thread is already operated or closed"

There is no terminal operation in the next java 8 thread. Isn't the next block supposed to be lazy, since I only have intermediate work and have not yet operated on a terminal operation. When I run this block, I get "the stream has already been operated or is closed." See https://ideone.com/naR7GB

Stream<String> s = Stream.of("A", "B");
s.map(String::toUpperCase);
s.map(String::toLowerCase);

Stack trace:

java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.<init>(AbstractPipeline.java:203)
at java.util.stream.ReferencePipeline.<init>(ReferencePipeline.java:94)
at java.util.stream.ReferencePipeline$StatelessOp.<init>(ReferencePipeline.java:618)
at java.util.stream.ReferencePipeline$3.<init>(ReferencePipeline.java:187)
at java.util.stream.ReferencePipeline.map(ReferencePipeline.java:186)
+4
source share
1 answer

You need to apply the second map()to the displayed instance:

s.map(String::toUpperCase).map(String::toLowerCase);

or

Stream<String> s = Stream.of("A", "B");
Stream<String> s2 = s.map(String::toUpperCase);
Stream<String> s3 = s2.map(String::toLowerCase);

How can you do only 1 operation in a single thread instance.

, s ! s2, s3, . , .

+7

Source: https://habr.com/ru/post/1695163/


All Articles