public static < T > Stream < List < T > > aggregate ( Stream < T > source , BiPredicate < T , T > predicate ) { return StreamSupport . stream ( new AggregatingSpliterator <> ( source . spliterator ( ) , ( a , e ) -> a . isEmpty ( ) || predicate . test ( a . get ( a . size ( ) - 1 ) , e ) ) , false ) . onClose ( source :: close ) ; }
Aggregates items from source stream into list of items while supplied predicate is true when evaluated on previous and current item . Can by seen as streaming alternative to Collectors . groupingBy when source stream is sorted by key .