I want to create a stream with random numbers. As soon as numbers fill a certain condition, I want how many times iterations have been. Therefore, either I want to have the size of the stream, or a collection from which I can read the size.
Here are my approaches:
random.ints(0, Integer.MAX_VALUE).anyMatch(a -> {return a < 20000;});
This only gives me a boolean once my condition is filled.
random.ints(0, Integer.MAX_VALUE).filter(a -> a < 20000).limit(1).count();
And this returns, obviously, 1. But I want to have size before filtering my result. I also tried several things with variable counting, but since lambdas capture them effectively from the outside, I have a problem with initialization.
Any help or hint appreciated
source share