I have been wondering about this for a week or two. I don't mean limit(), which stops after a predetermined number of elements. Maybe I might want to run a Stream until one of (numeric) elements equals 0. It is easy enough to do with loops:-
1: Let's imagine that maxCount is initialised to 0x7fff_ffff_ffff_ffffL or MIN_VALUE.2: Let's imagine you can reassign it without breaching the rules about effectively final.3: Let's imagine I can write even worse code. That would be quite an achievement, however.The nearest I had got to it was something like this:-
Horrible, isn't it? How would you do it?
I was looking through the Stream documentation an hour or so ago wondering whether they had added a terminateIf() method and, lo and behold, found
this method. It's new in Java9
Brilliant. Exactly the sort of thing I was thinking of.
I actually found
this method first, which appears to be the opposite of takeWhile, so I thought there was bound to be the counterpart, only if it starts with t it is at the bottom of the method summary.