Java 8 streams allow us to collect elements while grouping by an arbitrary constraint. For example:
Map<Type, List<MyThing>> grouped = stream
.collect(groupingBy(myThing -> myThing.type()));
However this has the drawback that the stream must be completely read through, so there is no chance of lazy evaluation of future operations on grouped
.
Is there a way to do a grouping operation to get something like Stream<Tuple<Type, Stream<MyThing>>>
? Is it even conceptually possible to group lazily in any language without evaluating the whole data set?