private <R> StreamEx<R> collapseInternal(BiPredicate<? super T, ? super T> collapsible, Function<T, R> mapper, BiFunction<R, T, R> accumulator, BinaryOperator<R> combiner) { CollapseSpliterator<T, R> spliterator = new CollapseSpliterator<>(collapsible, mapper, accumulator, combiner, spliterator()); return new StreamEx<>(spliterator, context); }
/** * Merge series of adjacent stream entries with equal keys combining the * corresponding values using the provided function. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate</a> * partial reduction operation. * * <p> * The key of the resulting entry is the key of the first merged entry. * * @param merger a non-interfering, stateless, associative function to merge * values of two adjacent entries which keys are equal. Note that it * can be applied to the results if previous merges. * @return a new {@code EntryStream} which keys are the keys of the original * stream and the values are values of the adjacent entries with the * same keys, combined using the provided merger function. * @see StreamEx#collapse(BiPredicate, BinaryOperator) * @since 0.5.5 */ public EntryStream<K, V> collapseKeys(BinaryOperator<V> merger) { BinaryOperator<Entry<K, V>> entryMerger = (e1, e2) -> new SimpleImmutableEntry<>(e1.getKey(), merger.apply(e1 .getValue(), e2.getValue())); return new EntryStream<>(new CollapseSpliterator<>(equalKeys(), Function.identity(), entryMerger, entryMerger, spliterator()), context); }
@Override public Spliterator<R> trySplit() { Spliterator<T> prefix = source.trySplit(); if (prefix == null) return null; Connector<T, R> newBox = new Connector<>(null, none(), this); synchronized (root) { CollapseSpliterator<T, R> result = new CollapseSpliterator<>(root, prefix, left, newBox); this.left = newBox; return result; } }