There is a much better answer than using
spliteratorUnknownSize directly, which is both easier and gets a better result.
Iterable has a method
spliterator(), so you should just use it to get your spliterator. In the worst case, it’s the same code (the default implementation uses
spliteratorUnknownSize), but in the most common case, where
Iterable it is already a collection, you will get a better splitter and therefore better streaming performance (maybe even good parallelism). It is also less code:
StreamSupport.stream(iterable.spliterator(), false) .filter(...) .moreStreamOps(...);
As you can see, getting a stream from a
Iterable (see also this question ) is not very painful.