Java 8 streams weren’t designed to support this kind of operation. From the jdk:
A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, “forked” streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream.
If you can store it in memory you can use Collectors.partitioningBy
if you have just two types and go by with a Map<Boolean, List>
. Otherwise use Collectors.groupingBy
.