1) Why exactly second forEach() is not working? What is the exact rule?
2) How can I reset the stream to run forEach on it once more? Do I always need to create another instance of IntStream like IntStream sm2 = str.chars();
to work on sm2 (sm2.forEach(...)
) or there is a way to "reset" sm ?
String str = "A B C D";
IntStream sm = str.chars();
sm.forEach(System.out::println); // works
// IllegalStateException: stream has already been operated upon or closed
sm.forEach(ch -> System.out.println(ch));
P.S. I know that forEach()
is a terminal operation, but I understand it in a way that forEach()
does not return the stream =>I cannot chain calls, that does not answer my question (I don't need to chain calls any further separating such calls with dots).