com.landawn.abacus.util.stream.Stream Maven / Gradle / Ivy
Show all versions of abacus-android Show documentation
/*
* Copyright (C) 2016, 2017, 2018, 2019 HaiYang Li
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.landawn.abacus.util.stream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.security.SecureRandom;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import com.landawn.abacus.DataSet;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.ParallelSupported;
import com.landawn.abacus.annotation.SequentialOnly;
import com.landawn.abacus.exception.UncheckedIOException;
import com.landawn.abacus.exception.UncheckedSQLException;
import com.landawn.abacus.util.AsyncExecutor;
import com.landawn.abacus.util.ByteIterator;
import com.landawn.abacus.util.CharIterator;
import com.landawn.abacus.util.Charsets;
import com.landawn.abacus.util.ContinuableFuture;
import com.landawn.abacus.util.DoubleIterator;
import com.landawn.abacus.util.Duration;
import com.landawn.abacus.util.ExceptionalStream;
import com.landawn.abacus.util.FloatIterator;
import com.landawn.abacus.util.Fn;
import com.landawn.abacus.util.Fn.Suppliers;
import com.landawn.abacus.util.IOUtil;
import com.landawn.abacus.util.ImmutableMap;
import com.landawn.abacus.util.Indexed;
import com.landawn.abacus.util.IntIterator;
import com.landawn.abacus.util.IntList;
import com.landawn.abacus.util.JdbcUtil;
import com.landawn.abacus.util.Keyed;
import com.landawn.abacus.util.LineIterator;
import com.landawn.abacus.util.ListMultimap;
import com.landawn.abacus.util.LongIterator;
import com.landawn.abacus.util.Matrix;
import com.landawn.abacus.util.Multimap;
import com.landawn.abacus.util.MutableBoolean;
import com.landawn.abacus.util.MutableInt;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.NoCachingNoUpdating;
import com.landawn.abacus.util.NoCachingNoUpdating.DisposableEntry;
import com.landawn.abacus.util.Nth;
import com.landawn.abacus.util.ObjIterator;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.Percentage;
import com.landawn.abacus.util.ShortIterator;
import com.landawn.abacus.util.Try;
import com.landawn.abacus.util.u.Holder;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BiPredicate;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.BooleanSupplier;
import com.landawn.abacus.util.function.ByteBiFunction;
import com.landawn.abacus.util.function.ByteNFunction;
import com.landawn.abacus.util.function.ByteTriFunction;
import com.landawn.abacus.util.function.CharBiFunction;
import com.landawn.abacus.util.function.CharNFunction;
import com.landawn.abacus.util.function.CharTriFunction;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.DoubleBiFunction;
import com.landawn.abacus.util.function.DoubleNFunction;
import com.landawn.abacus.util.function.DoubleTriFunction;
import com.landawn.abacus.util.function.FloatBiFunction;
import com.landawn.abacus.util.function.FloatNFunction;
import com.landawn.abacus.util.function.FloatTriFunction;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.IntBiFunction;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.IntNFunction;
import com.landawn.abacus.util.function.IntTriFunction;
import com.landawn.abacus.util.function.LongBiFunction;
import com.landawn.abacus.util.function.LongFunction;
import com.landawn.abacus.util.function.LongNFunction;
import com.landawn.abacus.util.function.LongSupplier;
import com.landawn.abacus.util.function.LongTriFunction;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.ShortBiFunction;
import com.landawn.abacus.util.function.ShortNFunction;
import com.landawn.abacus.util.function.ShortTriFunction;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToByteFunction;
import com.landawn.abacus.util.function.ToCharFunction;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToFloatFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.ToShortFunction;
import com.landawn.abacus.util.function.TriFunction;
import com.landawn.abacus.util.function.UnaryOperator;
import com.landawn.abacus.util.stream.ObjIteratorEx.QueuedIterator;
/**
* Note: This class includes codes copied from StreamEx: https://github.com/amaembo/streamex under Apache License, version 2.0.
*
*
* The Stream will be automatically closed after execution(A terminal method is executed/triggered).
*
* @param the type of the stream elements
* @see IntStream
* @see LongStream
* @see DoubleStream
*/
public abstract class Stream
extends StreamBase, Consumer super T>, List, Optional, Indexed, ObjIterator, Stream> {
static final Random RAND = new SecureRandom();
Stream(final boolean sorted, final Comparator super T> cmp, final Collection closeHandlers) {
super(sorted, cmp, closeHandlers);
}
@SequentialOnly
public Stream select(Class targetType) {
if (isParallel()) {
return (Stream) sequential().filter(Fn.instanceOf(targetType)).parallel(maxThreadNum(), splitor());
} else {
return (Stream) filter(Fn.instanceOf(targetType));
}
}
@ParallelSupported
public abstract Stream map(Function super T, ? extends R> mapper);
// public abstract Stream biMap(BiFunction super T, ? super T, ? extends R> mapper);
//
// /**
// * Returns a stream consisting of the results of applying the given function
// * to the every two adjacent elements of this stream.
// *
// *
// *
// * Stream.of("a", "b", "c", "d", "e").biMap((i, j) -> i + "-" + j).println();
// * // print out: [a-b, c-d, e-null]
// *
// *
// *
// * @param mapper
// * @param ignoreNotPaired flag to identify if need to ignore the last element when the total length of the stream is odd number. Default value is false
// * @return
// */
// public abstract Stream biMap(BiFunction super T, ? super T, ? extends R> mapper, boolean ignoreNotPaired);
//
// public abstract Stream triMap(TriFunction super T, ? super T, ? super T, ? extends R> mapper);
//
// /**
// * Returns a stream consisting of the results of applying the given function
// * to the every three adjacent elements of this stream.
// *
// *
// *
// * Stream.of("a", "b", "c", "d", "e").triMap((i, j, k) -> i + "-" + j + "-" + k).println();
// * // print out: [a-b-c, d-e-null]
// *
// *
// *
// * @param mapper
// * @param ignoreNotPaired flag to identify if need to ignore the last one or two elements when the total length of the stream is not multiple of 3. Default value is false
// * @return
// */
// public abstract Stream triMap(TriFunction super T, ? super T, ? super T, ? extends R> mapper, boolean ignoreNotPaired);
@ParallelSupported
public abstract Stream slidingMap(BiFunction super T, ? super T, R> mapper);
/**
* Slide with windowSize = 2
and the specified increment
, then map
by the specified mapper
.
*
* @param mapper
* @param increment
* @return
*/
@ParallelSupported
public abstract Stream slidingMap(BiFunction super T, ? super T, R> mapper, int increment);
@ParallelSupported
public abstract Stream slidingMap(BiFunction super T, ? super T, R> mapper, int increment, boolean ignoreNotPaired);
@ParallelSupported
public abstract Stream slidingMap(TriFunction super T, ? super T, ? super T, R> mapper);
/**
* Slide with windowSize = 3
and the specified increment
, then map
by the specified mapper
.
*
* @param mapper
* @param increment
* @return
*/
@ParallelSupported
public abstract Stream slidingMap(TriFunction super T, ? super T, ? super T, R> mapper, int increment);
@ParallelSupported
public abstract Stream slidingMap(TriFunction super T, ? super T, ? super T, R> mapper, int increment, boolean ignoreNotPaired);
/**
* Note: copied from StreamEx: https://github.com/amaembo/streamex
*
*
*
* Returns a stream consisting of results of applying the given function to
* the ranges created from the source elements.
*
*
*
* Stream.of("a", "ab", "ac", "b", "c", "cb").rangeMap((a, b) -> b.startsWith(a), (a, b) -> a + "->" + b).toList(); // a->ac, b->b, c->cb
*
*
*
*
* This is a quasi-intermediate
* partial reduction operation.
*
* @param the type of the resulting elements
* @param sameRange a non-interfering, stateless predicate to apply to
* the leftmost and next elements which returns true for elements
* which belong to the same range.
* @param mapper a non-interfering, stateless function to apply to the
* range borders and produce the resulting element. If value was
* not merged to the interval, then mapper will receive the same
* value twice, otherwise it will receive the leftmost and the
* rightmost values which were merged to the range.
* @return the new stream
* @see #collapse(BiPredicate, BinaryOperator)
*/
@SequentialOnly
public abstract Stream rangeMap(final BiPredicate super T, ? super T> sameRange, final BiFunction super T, ? super T, ? extends U> mapper);
@ParallelSupported
public abstract Stream mapFirst(Function super T, ? extends T> mapperForFirst);
@ParallelSupported
public abstract Stream mapFirstOrElse(Function super T, ? extends R> mapperForFirst, Function super T, ? extends R> mapperForElse);
@ParallelSupported
public abstract Stream mapLast(Function super T, ? extends T> mapperForLast);
@ParallelSupported
public abstract Stream mapLastOrElse(Function super T, ? extends R> mapperForLast, Function super T, ? extends R> mapperForElse);
@ParallelSupported
public abstract CharStream mapToChar(ToCharFunction super T> mapper);
@ParallelSupported
public abstract ByteStream mapToByte(ToByteFunction super T> mapper);
@ParallelSupported
public abstract ShortStream mapToShort(ToShortFunction super T> mapper);
@ParallelSupported
public abstract IntStream mapToInt(ToIntFunction super T> mapper);
@ParallelSupported
public abstract LongStream mapToLong(ToLongFunction super T> mapper);
@ParallelSupported
public abstract FloatStream mapToFloat(ToFloatFunction super T> mapper);
@ParallelSupported
public abstract DoubleStream mapToDouble(ToDoubleFunction super T> mapper);
// public abstract EntryStream mapToEntry();
@ParallelSupported
public abstract EntryStream mapToEntry(Function super T, ? extends Map.Entry extends K, ? extends V>> mapper);
@ParallelSupported
public abstract EntryStream mapToEntry(Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper);
// public abstract Stream mapp(Function super T, ? extends Optional extends U>> mapper);
@ParallelSupported
public abstract Stream flatMap(Function super T, ? extends Stream extends R>> mapper);
@ParallelSupported
public abstract Stream flattMap(Function super T, ? extends Collection extends R>> mapper);
@ParallelSupported
public abstract Stream flatMapp(Function super T, R[]> mapper);
@ParallelSupported
public abstract CharStream flatMapToChar(Function super T, ? extends CharStream> mapper);
@ParallelSupported
public abstract CharStream flattMapToChar(Function super T, char[]> mapper);
@ParallelSupported
public abstract ByteStream flatMapToByte(Function super T, ? extends ByteStream> mapper);
@ParallelSupported
public abstract ByteStream flattMapToByte(Function super T, byte[]> mapper);
@ParallelSupported
public abstract ShortStream flatMapToShort(Function super T, ? extends ShortStream> mapper);
@ParallelSupported
public abstract ShortStream flattMapToShort(Function super T, short[]> mapper);
@ParallelSupported
public abstract IntStream flatMapToInt(Function super T, ? extends IntStream> mapper);
@ParallelSupported
public abstract IntStream flattMapToInt(Function super T, int[]> mapper);
@ParallelSupported
public abstract LongStream flatMapToLong(Function super T, ? extends LongStream> mapper);
@ParallelSupported
public abstract LongStream flattMapToLong(Function super T, long[]> mapper);
@ParallelSupported
public abstract FloatStream flatMapToFloat(Function super T, ? extends FloatStream> mapper);
@ParallelSupported
public abstract FloatStream flattMapToFloat(Function super T, float[]> mapper);
@ParallelSupported
public abstract DoubleStream flatMapToDouble(Function super T, ? extends DoubleStream> mapper);
@ParallelSupported
public abstract DoubleStream flattMapToDouble(Function super T, double[]> mapper);
@ParallelSupported
public abstract EntryStream flatMapToEntry(Function super T, ? extends Stream extends Map.Entry extends K, ? extends V>>> mapper);
@ParallelSupported
public abstract EntryStream flattMapToEntry(Function super T, ? extends Map extends K, ? extends V>> mapper);
@ParallelSupported
public abstract EntryStream flatMappToEntry(Function super T, ? extends EntryStream extends K, ? extends V>> mapper);
@ParallelSupported
public abstract Stream>> groupBy(final Function super T, ? extends K> keyMapper);
@ParallelSupported
public abstract Stream>> groupBy(final Function super T, ? extends K> keyMapper,
final Supplier extends Map>> mapFactory);
/**
*
* @param keyMapper
* @param valueMapper
* @return
* @see Collectors#toMultimap(Function, Function)
*/
@ParallelSupported
public abstract Stream>> groupBy(Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper);
/**
*
* @param keyMapper
* @param valueMapper
* @param mapFactory
* @return
* @see Collectors#toMultimap(Function, Function, Supplier)
*/
@ParallelSupported
public abstract Stream>> groupBy(Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper,
Supplier extends Map>> mapFactory);
@ParallelSupported
public abstract Stream> groupBy(final Function super T, ? extends K> keyMapper, final Collector super T, A, D> downstream);
@ParallelSupported
public abstract Stream> groupBy(final Function super T, ? extends K> keyMapper, final Collector super T, A, D> downstream,
final Supplier extends Map> mapFactory);
@ParallelSupported
public abstract Stream> groupBy(final Function super T, ? extends K> keyMapper,
final Function super T, ? extends V> valueMapper, final Collector super V, A, D> downstream);
@ParallelSupported
public abstract Stream> groupBy(final Function super T, ? extends K> keyMapper,
final Function super T, ? extends V> valueMapper, final Collector super V, A, D> downstream, final Supplier extends Map> mapFactory);
@ParallelSupported
public abstract Stream> groupBy(final Function super T, ? extends K> keyMapper, final Function super T, ? extends V> valueMapper,
BinaryOperator mergeFunction);
@ParallelSupported
public abstract Stream> groupBy(final Function super T, ? extends K> keyMapper, final Function super T, ? extends V> valueMapper,
final BinaryOperator mergeFunction, final Supplier extends Map> mapFactory);
// @ParallelSupported
// public abstract Stream>> flatGroupBy(final Function super T, ? extends Stream extends K>> flatKeyMapper);
//
// @ParallelSupported
// public abstract Stream>> flatGroupBy(final Function super T, ? extends Stream extends K>> flatKeyMapper,
// final Supplier extends Map>> mapFactory);
//
// /**
// *
// * @param flatKeyMapper
// * @param valueMapper
// * @return
// * @see Collectors#toMultimap(Function, Function)
// */
// @ParallelSupported
// public abstract Stream>> flatGroupBy(Function super T, ? extends Stream extends K>> flatKeyMapper,
// BiFunction super K, ? super T, ? extends V> valueMapper);
//
// /**
// *
// * @param flatKeyMapper
// * @param valueMapper
// * @param mapFactory
// * @return
// * @see Collectors#toMultimap(Function, Function, Supplier)
// */
// @ParallelSupported
// public abstract Stream>> flatGroupBy(Function super T, ? extends Stream extends K>> flatKeyMapper,
// BiFunction super K, ? super T, ? extends V> valueMapper, Supplier extends Map>> mapFactory);
//
// @ParallelSupported
// public abstract Stream> flatGroupBy(final Function super T, ? extends Stream extends K>> flatKeyMapper,
// final Collector super T, A, D> downstream);
//
// @ParallelSupported
// public abstract Stream> flatGroupBy(final Function super T, ? extends Stream extends K>> flatKeyMapper,
// final Collector super T, A, D> downstream, final Supplier extends Map> mapFactory);
//
// @ParallelSupported
// public abstract Stream> flatGroupBy(final Function super T, ? extends Stream extends K>> flatKeyMapper,
// final BiFunction super K, ? super T, ? extends V> valueMapper, final Collector super V, A, D> downstream);
//
// @ParallelSupported
// public abstract Stream> flatGroupBy(final Function super T, ? extends Stream extends K>> flatKeyMapper,
// final BiFunction super K, ? super T, ? extends V> valueMapper, final Collector super V, A, D> downstream,
// final Supplier extends Map> mapFactory);
//
// @ParallelSupported
// public abstract Stream> flatGroupBy(final Function super T, ? extends Stream extends K>> flatKeyMapper,
// final BiFunction super K, ? super T, ? extends V> valueMapper, BinaryOperator mergeFunction);
//
// @ParallelSupported
// public abstract Stream> flatGroupBy(final Function super T, ? extends Stream extends K>> flatKeyMapper,
// final BiFunction super K, ? super T, ? extends V> valueMapper, final BinaryOperator mergeFunction,
// final Supplier extends Map> mapFactory);
//
// @ParallelSupported
// public abstract Stream>> flattGroupBy(final Function super T, ? extends Collection extends K>> flatKeyMapper);
//
// @ParallelSupported
// public abstract Stream>> flattGroupBy(final Function super T, ? extends Collection extends K>> flatKeyMapper,
// final Supplier extends Map>> mapFactory);
//
// /**
// *
// * @param flatKeyMapper
// * @param valueMapper
// * @return
// * @see Collectors#toMultimap(Function, Function)
// */
// @ParallelSupported
// public abstract Stream>> flattGroupBy(Function super T, ? extends Collection extends K>> flatKeyMapper,
// BiFunction super K, ? super T, ? extends V> valueMapper);
//
// /**
// *
// * @param flatKeyMapper
// * @param valueMapper
// * @param mapFactory
// * @return
// * @see Collectors#toMultimap(Function, Function, Supplier)
// */
// @ParallelSupported
// public abstract Stream>> flattGroupBy(Function super T, ? extends Collection extends K>> flatKeyMapper,
// BiFunction super K, ? super T, ? extends V> valueMapper, Supplier extends Map>> mapFactory);
//
// @ParallelSupported
// public abstract Stream> flattGroupBy(final Function super T, ? extends Collection extends K>> flatKeyMapper,
// final Collector super T, A, D> downstream);
//
// @ParallelSupported
// public abstract Stream> flattGroupBy(final Function super T, ? extends Collection extends K>> flatKeyMapper,
// final Collector super T, A, D> downstream, final Supplier extends Map> mapFactory);
//
// @ParallelSupported
// public abstract Stream> flattGroupBy(final Function super T, ? extends Collection extends K>> flatKeyMapper,
// final BiFunction super K, ? super T, ? extends V> valueMapper, final Collector super V, A, D> downstream);
//
// @ParallelSupported
// public abstract Stream> flattGroupBy(final Function super T, ? extends Collection extends K>> flatKeyMapper,
// final BiFunction super K, ? super T, ? extends V> valueMapper, final Collector super V, A, D> downstream,
// final Supplier extends Map> mapFactory);
//
// @ParallelSupported
// public abstract Stream> flattGroupBy(final Function super T, ? extends Collection extends K>> flatKeyMapper,
// final BiFunction super K, ? super T, ? extends V> valueMapper, BinaryOperator mergeFunction);
//
// @ParallelSupported
// public abstract Stream> flattGroupBy(final Function super T, ? extends Collection extends K>> flatKeyMapper,
// final BiFunction super K, ? super T, ? extends V> valueMapper, final BinaryOperator mergeFunction,
// final Supplier extends Map> mapFactory);
/**
*
* @param predicate
* @return
* @see Collectors#partitioningBy(Predicate)
*/
@ParallelSupported
public abstract Stream>> partitionBy(final Predicate super T> predicate);
/**
*
* @param predicate
* @param downstream
* @return
* @see Collectors#partitioningBy(Predicate, Collector)
*/
@ParallelSupported
public abstract Stream> partitionBy(final Predicate super T> predicate, final Collector super T, A, D> downstream);
@ParallelSupported
public Stream> countBy(final Function super T, ? extends K> keyMapper) {
return groupBy(keyMapper, Collectors.countingInt());
}
@ParallelSupported
public abstract EntryStream> groupByToEntry(final Function super T, ? extends K> keyMapper);
@ParallelSupported
public abstract EntryStream> groupByToEntry(final Function super T, ? extends K> keyMapper,
final Supplier extends Map>> mapFactory);
/**
*
* @param keyMapper
* @param valueMapper
* @return
* @see Collectors#toMultimap(Function, Function)
*/
@ParallelSupported
public abstract EntryStream> groupByToEntry(Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper);
/**
*
* @param keyMapper
* @param valueMapper
* @param mapFactory
* @return
* @see Collectors#toMultimap(Function, Function, Supplier)
*/
@ParallelSupported
public abstract EntryStream> groupByToEntry(Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper,
Supplier extends Map>> mapFactory);
@ParallelSupported
public abstract EntryStream groupByToEntry(final Function super T, ? extends K> keyMapper, final Collector super T, A, D> downstream);
@ParallelSupported
public abstract EntryStream groupByToEntry(final Function super T, ? extends K> keyMapper, final Collector super T, A, D> downstream,
final Supplier extends Map> mapFactory);
@ParallelSupported
public abstract EntryStream groupByToEntry(final Function super T, ? extends K> keyMapper,
final Function super T, ? extends V> valueMapper, final Collector super V, A, D> downstream);
@ParallelSupported
public abstract EntryStream groupByToEntry(final Function super T, ? extends K> keyMapper,
final Function super T, ? extends V> valueMapper, final Collector super V, A, D> downstream, final Supplier extends Map> mapFactory);
@ParallelSupported
public abstract EntryStream groupByToEntry(final Function super T, ? extends K> keyMapper,
final Function super T, ? extends V> valueMapper, BinaryOperator mergeFunction);
@ParallelSupported
public abstract EntryStream groupByToEntry(final Function super T, ? extends K> keyMapper,
final Function super T, ? extends V> valueMapper, final BinaryOperator mergeFunction, final Supplier extends Map> mapFactory);
/**
*
* @param predicate
* @return
* @see Collectors#partitioningBy(Predicate)
*/
@ParallelSupported
public abstract EntryStream> partitionByToEntry(final Predicate super T> predicate);
/**
*
* @param predicate
* @param downstream
* @return
* @see Collectors#partitioningBy(Predicate, Collector)
*/
@ParallelSupported
public abstract EntryStream partitionByToEntry(final Predicate super T> predicate, final Collector super T, A, D> downstream);
@ParallelSupported
public EntryStream countByToEntry(final Function super T, ? extends K> keyMapper) {
return groupByToEntry(keyMapper, Collectors.countingInt());
}
@SequentialOnly
public abstract Stream> collapse(final BiPredicate super T, ? super T> collapsible);
@SequentialOnly
public abstract > Stream collapse(final BiPredicate super T, ? super T> collapsible, Supplier extends C> supplier);
/**
* Merge series of adjacent elements which satisfy the given predicate using
* the merger function and return a new stream.
*
* Example:
*
*
* Stream.of(new Integer[0]).collapse((a, b) -> a < b, (a, b) -> a + b) => []
* Stream.of(1).collapse((a, b) -> a < b, (a, b) -> a + b) => [1]
* Stream.of(1, 2).collapse((a, b) -> a < b, (a, b) -> a + b) => [3]
* Stream.of(1, 2, 3).collapse((a, b) -> a < b, (a, b) -> a + b) => [6]
* Stream.of(1, 2, 3, 3, 2, 1).collapse((a, b) -> a < b, (a, b) -> a + b) => [6, 3, 2, 1]
*
*
*
*
* This method only run sequentially, even in parallel stream.
*
* @param collapsible
* @param mergeFunction
* @return
*/
@SequentialOnly
public abstract Stream collapse(final BiPredicate super T, ? super T> collapsible, final BiFunction super T, ? super T, T> mergeFunction);
@SequentialOnly
public abstract Stream collapse(final BiPredicate super T, ? super T> collapsible, final U init, final BiFunction op);
@SequentialOnly
public abstract Stream collapse(final BiPredicate super T, ? super T> collapsible, final Supplier supplier,
final BiConsumer super R, ? super T> accumulator);
/**
* Merge series of adjacent elements which satisfy the given predicate using
* the merger function and return a new stream.
*
* Example:
*
*
* Stream.of(new Integer[0]).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => []
* Stream.of(1).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => [1]
* Stream.of(1, 2).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => [3]
* Stream.of(1, 2, 3).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => [6]
* Stream.of(1, 2, 3, 3, 2, 1).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => [6, 3, 2, 1]
*
*
*
*
* This method only run sequentially, even in parallel stream.
*
* @param collapsible
* @param collector
* @return
*/
@SequentialOnly
public abstract Stream collapse(final BiPredicate super T, ? super T> collapsible, final Collector super T, A, R> collector);
/**
* Returns a {@code Stream} produced by iterative application of a accumulation function
* to an initial element {@code init} and next element of the current stream.
* Produces a {@code Stream} consisting of {@code init}, {@code acc(init, value1)},
* {@code acc(acc(init, value1), value2)}, etc.
*
* This is an intermediate operation.
*
*
Example:
*
*
* Stream.of(new Integer[0]).scan((a, b) -> a + b) => []
* Stream.of(1).scan((a, b) -> a + b) => [1]
* Stream.of(1, 2).scan((a, b) -> a + b) => [1, 3]
* Stream.of(1, 2, 3).scan((a, b) -> a + b) => [1, 3, 6]
* Stream.of(1, 2, 3, 3, 2, 1).scan((a, b) -> a + b) => [1, 3, 6, 9, 11, 12]
*
*
*
*
* This method only run sequentially, even in parallel stream.
*
* @param accumulator the accumulation function
* @return the new stream which has the extract same size as this stream.
*/
@SequentialOnly
public abstract Stream scan(final BiFunction super T, ? super T, T> accumulator);
/**
* Returns a {@code Stream} produced by iterative application of a accumulation function
* to an initial element {@code init} and next element of the current stream.
* Produces a {@code Stream} consisting of {@code init}, {@code acc(init, value1)},
* {@code acc(acc(init, value1), value2)}, etc.
*
* This is an intermediate operation.
*
*
Example:
*
*
* Stream.of(new Integer[0]).scan(10, (a, b) -> a + b) => []
* Stream.of(1).scan(10, (a, b) -> a + b) => [11]
* Stream.of(1, 2).scan(10, (a, b) -> a + b) => [11, 13]
* Stream.of(1, 2, 3).scan(10, (a, b) -> a + b) => [11, 13, 16]
* Stream.of(1, 2, 3, 3, 2, 1).scan(10, (a, b) -> a + b) => [11, 13, 16, 19, 21, 22]
*
*
*
*
* This method only run sequentially, even in parallel stream.
*
* @param init the initial value. it's only used once by accumulator
to calculate the fist element in the returned stream.
* It will be ignored if this stream is empty and won't be the first element of the returned stream.
*
* @param accumulator the accumulation function
* @return the new stream which has the extract same size as this stream.
*/
@SequentialOnly
public abstract Stream scan(final U init, final BiFunction accumulator);
/**
*
* @param init
* @param accumulator
* @param initIncluded
* @return
*/
@SequentialOnly
public abstract Stream scan(final U init, final BiFunction accumulator, final boolean initIncluded);
/**
* Returns Stream of Stream with consecutive sub sequences of the elements, each of the same size (the final sequence may be smaller).
*
*
* This method only run sequentially, even in parallel stream.
*
* @param chunkSize the desired size of each sub sequence (the last may be smaller).
* @return
*/
@SequentialOnly
public abstract Stream> splitToSet(int chunkSize);
/**
* Returns Stream of Stream with consecutive sub sequences of the elements, each of the same size (the final sequence may be smaller).
*
*
* This method only run sequentially, even in parallel stream.
*
* @param chunkSize the desired size of each sub sequence (the last may be smaller).
* @param collectionSupplier
* @return
*/
@SequentialOnly
public abstract > Stream split(int chunkSize, IntFunction extends C> collectionSupplier);
/**
*
* @param chunkSize the desired size of each sub sequence (the last may be smaller).
* @param collector
* @return
*/
@SequentialOnly
public abstract Stream split(int chunkSize, Collector super T, A, R> collector);
@SequentialOnly
public abstract Stream> splitToSet(Predicate super T> predicate);
@SequentialOnly
public abstract > Stream split(Predicate super T> predicate, Supplier extends C> collectionSupplier);
@SequentialOnly
public abstract Stream split(Predicate super T> predicate, Collector super T, A, R> collector);
@SequentialOnly
public abstract Stream splitAt(int where, Collector super T, A, R> collector);
@SequentialOnly
public abstract Stream splitBy(Predicate super T> where, Collector super T, A, R> collector);
@SequentialOnly
public abstract > Stream sliding(int windowSize, IntFunction extends C> collectionSupplier);
@SequentialOnly
public abstract > Stream sliding(int windowSize, int increment, IntFunction extends C> collectionSupplier);
@SequentialOnly
public abstract Stream sliding(int windowSize, Collector super T, A, R> collector);
@SequentialOnly
public abstract Stream sliding(int windowSize, int increment, Collector super T, A, R> collector);
/**
* Split this stream by the specified duration.
*
* @param duration
* @return
* @see Fn#window(Duration, LongSupplier)
*/
@SequentialOnly
public abstract Stream> window(Duration duration);
/**
*
* @param duration
* @param startTime
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream> window(Duration duration, LongSupplier startTime);
/**
*
* @param duration
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream> windowToList(Duration duration);
/**
*
* @param duration
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream> windowToSet(Duration duration);
/**
*
* @param duration
* @param collectionSupplier
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract > Stream window(Duration duration, Supplier extends C> collectionSupplier);
/**
*
* @param duration
* @param startTime
* @param collectionSupplier
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract > Stream window(Duration duration, LongSupplier startTime, Supplier extends C> collectionSupplier);
/**
*
* @param duration
* @param collector
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream window(Duration duration, Collector super T, A, R> collector);
/**
*
* @param duration
* @param startTime
* @param collector
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream window(Duration duration, LongSupplier startTime, Collector super T, A, R> collector);
/**
*
* @param duration
* @param incrementInMillis
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream> window(Duration duration, long incrementInMillis);
/**
*
* @param duration
* @param incrementInMillis
* @param startTime
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream> window(Duration duration, long incrementInMillis, LongSupplier startTime);
/**
*
* @param duration
* @param incrementInMillis
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream> windowToList(Duration duration, long incrementInMillis);
/**
*
* @param duration
* @param incrementInMillis
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream> windowToSet(Duration duration, long incrementInMillis);
/**
*
* @param duration
* @param incrementInMillis
* @param collectionSupplier
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract > Stream window(Duration duration, long incrementInMillis, Supplier extends C> collectionSupplier);
/**
*
* @param duration
* @param incrementInMillis
* @param startTime
* @param collectionSupplier
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract > Stream window(Duration duration, long incrementInMillis, LongSupplier startTime,
Supplier extends C> collectionSupplier);
/**
*
* @param duration
* @param incrementInMillis
* @param collector
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream window(Duration duration, long incrementInMillis, Collector super T, A, R> collector);
/**
*
* @param duration
* @param incrementInMillis
* @param startTime
* @param collector
* @return
* @see #window(Duration)
* @see Fn#window(Duration, LongSupplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream window(Duration duration, long incrementInMillis, LongSupplier startTime, Collector super T, A, R> collector);
/**
*
* @param maxWindowSize
* @param maxDuration
* @return
* @see #window(int, Duration, LongSupplier, Supplier)
* @see Fn#window(int, Duration, LongSupplier, Supplier)
*/
@SequentialOnly
public abstract Stream> window(int maxWindowSize, Duration maxDuration);
/**
*
* @param maxWindowSize
* @param maxDuration
* @param startTime
* @return
* @see #window(int, Duration, LongSupplier, Supplier)
* @see Fn#window(int, Duration, LongSupplier, Supplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract Stream> window(int maxWindowSize, Duration maxDuration, LongSupplier startTime);
/**
*
* @param maxWindowSize
* @param maxDuration
* @param collectionSupplier
* @return
* @see #window(int, Duration, LongSupplier, Supplier)
* @see Fn#window(int, Duration, LongSupplier, Supplier)
* @see #sliding(int, int, Collector)
*/
@SequentialOnly
public abstract