/** * Merge series of adjacent elements which satisfy the given predicate using * the merger function and return a new stream. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate</a> * partial reduction operation. * * <p> * This operation is equivalent to * {@code collapse(collapsible, Collectors.reducing(merger)).map(Optional::get)} * , but more efficient. * * @param collapsible a non-interfering, stateless predicate to apply to the * pair of adjacent elements of the input stream which returns true * for elements which are collapsible. * @param merger a non-interfering, stateless, associative function to merge * two adjacent elements for which collapsible predicate returned * true. Note that it can be applied to the results if previous * merges. * @return the new stream * @since 0.3.1 */ public StreamEx<T> collapse(BiPredicate<? super T, ? super T> collapsible, BinaryOperator<T> merger) { return collapseInternal(collapsible, Function.identity(), merger, merger); }
return collapseInternal(sameGroup, Collections::singletonList, (acc, t) -> { if (!(acc instanceof ArrayList)) { T old = acc.get(0);
return collapseInternal(sameInterval, PairBox::single, (box, t) -> { box.b = t; return box;
Supplier<A> supplier = collector.supplier(); BiConsumer<A, ? super T> accumulator = collector.accumulator(); StreamEx<A> stream = collapseInternal(collapsible, t -> { A acc = supplier.get(); accumulator.accept(acc, t);
/** * Collapses adjacent equal elements and returns an {@link EntryStream} * where keys are input elements and values specify how many elements were * collapsed. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate</a> * partial reduction operation. * * <p> * For sorted input {@code runLengths().toMap()} is the same as * {@code groupingBy(Function.identity(), Collectors.counting())}, but may * perform faster. For unsorted input the resulting stream may contain * repeating keys. * * @return the new stream * @since 0.3.3 */ public EntryStream<T, Long> runLengths() { return new EntryStream<>(collapseInternal(Objects::equals, t -> new ObjLongBox<>(t, 1L), (acc, t) -> { acc.b++; return acc; }, (e1, e2) -> { e1.b += e2.b; return e1; }), context); }