Practice mode Java

What happens when the Java code Stream<Integer> s = Stream.of(1); s.count(); s.count(); is run?

Correct answer A. The second count throws an IllegalStateException

Explanation

A stream may be operated on only once. The first terminal operation consumes it, so linking or consuming it again throws IllegalStateException at run time. Streams are not containers of data but single-use pipelines over a source. The usual remedy is to create a fresh stream from the source each time one is needed.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.