One minor tweak is, there's a shorter way to express the same thing:
While it's a little more concise, it does have the same issue that the original did, in the sense that it looks at the entire stream. There's not a good built-in solution that I found. But, I did manage to come up with a solution that seems to work:
The parallel() seems to be necessary, at least in my tests, to get the stream to return a Spliterator that will actually do a split. Otherwise I kept getting the split() returning null, which means there are no further subdivisions possible. Interestingly, we're not actually
using multiple threads to evaluate the spliterator. We're just telling the stream to act as if we
could use parallel streams, thereby encouraging the split() to allow us to split off some of elements, even if it thinks it could do this faster in a single
thread.
To better understand how it works and to experiment with it (and verify that it really can be much faster than reading everything):
Output:
Note that it only evaluates the last element. For some streams, it might end up having to evaluate more, in cases where it's hard for the Spliterator to estimate size correctly and some splits might not contain any actual elements.