the type of array
* @param the type of predicate
* @param the type of consumer
* @param the type of PrimitiveList/List
* @param the type of Optional
* @param the type of Indexed
* @param the type of Iterator
* @param the type of of the stream implementing {@code BaseStream}
* @see Stream
* @see EntryStream
* @see IntStream
* @see LongStream
* @see DoubleStream
* @see com.landawn.abacus.util.ExceptionalStream
* @see Collectors
* @see com.landawn.abacus.util.Fn
* @see com.landawn.abacus.util.Comparators
*/
@com.landawn.abacus.annotation.Immutable
@LazyEvaluation
public interface BaseStream, S extends BaseStream>
extends Closeable, Immutable {
/**
* Returns a stream consisting of the elements of this stream that match the given predicate.
*
* @param predicate
* @return
*/
@ParallelSupported
@IntermediateOp
S filter(P predicate);
/**
* Returns a stream consisting of the elements of this stream that match the given predicate.
*
* @param predicate
* @param actionOnDroppedItem
* @return
*/
@Beta
@ParallelSupported
@IntermediateOp
S filter(P predicate, C actionOnDroppedItem);
/**
* Keep the elements until the given predicate returns false.
* The stream should be sorted, which means if x is the first element: predicate.text(x)
returns false, any element y behind x: predicate.text(y)
should returns false.
*
* In parallel Streams, the elements after the first element which predicate
returns false may be tested by predicate too.
*
*
* For example:
*
* // For sequential stream:
* Stream.of(1, 2, 3, 4, 5, 6).takeWhile(it -> it < 5).toList() ===> [1, 2, 3, 4]
* Stream.of(1, 2, 5, 6, 3, 4).takeWhile(it -> it < 5).toList() ===> [1, 2]
* Stream.of(5, 6, 1, 2, 3, 4).takeWhile(it -> it < 5).toList() ===> []
*
*
* // For parallel stream:
* Stream.of(1, 2, 3, 4, 5, 6).parallel().takeWhile(it -> it < 5).toList() ===> [1, 2, 3, 4] // Order could be different since it's in parallel stream.
* Stream.of(1, 2, 5, 6, 3, 4).parallel().takeWhile(it -> it < 5).toList() ===> [1, 2] // or [1, 2, 3] or [1, 2, 3, 4] // Order could be different since it's in parallel stream.
* Stream.of(5, 6, 1, 2, 3, 4).parallel().takeWhile(it -> it < 5).toList() ===> any sub set of [1, 2, 3, 4], including [] // Order could be different since it's in parallel stream.
*
*
* @param predicate
* @return
*/
@ParallelSupported
@IntermediateOp
S takeWhile(P predicate);
/**
* Remove the elements until the given predicate returns false.
* The stream should be sorted, which means if x is the first element: predicate.text(x)
returns true, any element y behind x: predicate.text(y)
should returns true.
*
* In parallel Streams, the elements after the first element which predicate
returns false may be tested by predicate too.
*
*
* For example:
*
* // For sequential stream:
* Stream.of(1, 2, 3, 4, 5, 6).dropWhile(it -> it < 4).toList() ===> [4, 5, 6]
* Stream.of(1, 2, 5, 6, 3, 4).dropWhile(it -> it < 4).toList() ===> [5, 6, 3, 4]
* Stream.of(5, 6, 1, 2, 3, 4).dropWhile(it -> it < 4).toList() ===> [5, 6, 1, 2, 3, 4]
*
*
* // For parallel stream:
* Stream.of(1, 2, 3, 4, 5, 6).parallel().dropWhile(it -> it < 4).toList() ===> [4, 5, 6] // Order could be different since it's in parallel stream.
* Stream.of(1, 2, 5, 6, 3, 4).parallel().dropWhile(it -> it < 4).toList() ===> [5, 6, 4] // or [5, 6, 3, 4] // Order could be different since it's in parallel stream.
* Stream.of(5, 6, 1, 2, 3, 4).parallel().dropWhile(it -> it < 4).toList() ===> [5, 6] + any sub set of [1, 2, 3, 4] // Order could be different since it's in parallel stream.
*
*
* @param predicate
* @return
*/
@ParallelSupported
@IntermediateOp
S dropWhile(P predicate);
/**
* Remove the elements until the given predicate returns false. The stream should be sorted, which means if x is the first element: predicate.text(x)
returns true, any element y behind x: predicate.text(y)
should returns true.
*
* In parallel Streams, the elements after the first element which predicate
returns false may be tested by predicate too.
*
*
* @param predicate
* @param actionOnDroppedItem
* @return {@link #dropWhile(Object)}
*/
@Beta
@ParallelSupported
@IntermediateOp
S dropWhile(P predicate, C actionOnDroppedItem);
/**
*
* @param predicate
* @return
* @see #dropWhile(Object)
*/
@Beta
@ParallelSupported
@IntermediateOp
S skipUntil(P predicate);
/**
*
* @param predicate
* @return
* @deprecated
*/
@ParallelSupported
@IntermediateOp
@Deprecated
S removeIf(P predicate);
/**
*
* @param predicate
* @param actionOnDroppedItem
* @return
* @deprecated
*/
@ParallelSupported
@IntermediateOp
@Deprecated
S removeIf(P predicate, C actionOnDroppedItem);
/**
* Returns Stream of {@code S} with consecutive sub sequences of the elements, each of the same size (the final sequence may be smaller).
*
* @param chunkSize the desired size of each sub sequence (the last may be smaller).
* @return
*/
@SequentialOnly
@IntermediateOp
Stream split(int chunkSize);
/**
* Returns Stream of {@code PL} with consecutive sub sequences of the elements, each of the same size (the final sequence may be smaller).
*
*
* This method only runs sequentially, even in parallel stream.
*
* @param chunkSize the desired size of each sub sequence (the last may be smaller).
* @return
*/
@SequentialOnly
@IntermediateOp
Stream splitToList(int chunkSize);
/**
* Split the stream by the specified predicate.
*
*
* This stream should be sorted by value which is used to verify the border.
*
* This method only runs sequentially, even in parallel stream.
*
* @param predicate
* @return
*/
@SequentialOnly
@IntermediateOp
Stream split(final P predicate);
/**
* Split the stream by the specified predicate.
*
* This method only runs sequentially, even in parallel stream.
*
* @param predicate
* @return
*/
@SequentialOnly
@IntermediateOp
Stream splitToList(final P predicate);
/**
* Split the stream into two pieces at where
.
* The first piece will be loaded into memory.
*
* @param where
* @return
*/
@SequentialOnly
@IntermediateOp
Stream splitAt(int where);
/**
* Split the stream into two pieces at where
turns to {@code true}.
* The first piece will be loaded into memory.
*
*
*
* Stream.of(1, 3, 2, 4, 2, 5).splitAt(it -> it >= 4).forEach(s -> s.println()); // [1, 3, 2], [4, 2, 5]
*
*
*
* @param where
* @return
*/
@SequentialOnly
@IntermediateOp
Stream splitAt(P where);
/**
*
* @param windowSize
* @return
* @see #sliding(int, int)
*/
@SequentialOnly
@IntermediateOp
Stream sliding(int windowSize);
/**
*
* @param windowSize
* @return
* @see #sliding(int, int)
*/
@SequentialOnly
@IntermediateOp
Stream slidingToList(int windowSize);
/**
* Stream.of(1, 2, 3, 4, 5, 6, 7, 8).sliding(3, 1).forEach(Stream::println)
*
output:
* [1, 2, 3]
* [2, 3, 4]
* [3, 4, 5]
* [4, 5, 6]
* [5, 6, 7]
* [6, 7, 8]
*
*
============================================================================
* Stream.of(1, 2, 3, 4, 5, 6, 7, 8).sliding(3, 3).forEach(Stream::println)
*
output:
* [1, 2, 3]
* [4, 5, 6]
* [7, 8]
*
*
============================================================================
* Stream.of(1, 2, 3, 4, 5, 6, 7, 5).sliding(3, 5).forEach(Stream::println)
*
output:
* [1, 2, 3]
* [6, 7, 8]
*
*
* This method only runs sequentially, even in parallel stream.
*
* @param windowSize
* @param increment
* @return
*/
@SequentialOnly
@IntermediateOp
Stream sliding(int windowSize, int increment);
/**
*
* @param windowSize
* @param increment
* @return
* @see #sliding(int, int)
*/
@SequentialOnly
@IntermediateOp
Stream slidingToList(int windowSize, int increment);
/**
*
* This method only runs sequentially, even in parallel stream.
*
* @param c
* @return
* @see IntList#intersection(IntList)
*/
@SequentialOnly
@IntermediateOp
S intersection(Collection> c);
/**
*
* This method only runs sequentially, even in parallel stream.
*
* @param c
* @return
* @see IntList#difference(IntList)
*/
@SequentialOnly
@IntermediateOp
S difference(Collection> c);
/**
*
* This method only runs sequentially, even in parallel stream.
*
* @param c
* @return
* @see IntList#symmetricDifference(IntList)
*/
@SequentialOnly
@IntermediateOp
S symmetricDifference(Collection c);
/**
*
*
* This method only runs sequentially, even in parallel stream and all elements will be loaded to memory.
*
* @return
*/
@SequentialOnly
@IntermediateOp
@TerminalOpTriggered
S reversed();
/**
*
*
* This method only runs sequentially, even in parallel stream and all elements will be loaded to memory.
*
* @return
*/
@SequentialOnly
@IntermediateOp
@TerminalOpTriggered
S rotated(int distance);
/**
*
*
* This method only runs sequentially, even in parallel stream and all elements will be loaded to memory.
*
* @return
*/
@SequentialOnly
@IntermediateOp
@TerminalOpTriggered
S shuffled();
/**
*
*
* This method only runs sequentially, even in parallel stream and all elements will be loaded to memory.
*
* @return
*/
@SequentialOnly
@IntermediateOp
@TerminalOpTriggered
S shuffled(Random rnd);
/**
* Returns a stream consisting of the distinct elements of this stream.
*
* @return
*/
@SequentialOnly
@IntermediateOp
S distinct();
/**
* Returns a stream consisting of the elements of this stream in sorted order.
*
*
* All elements will be loaded to memory.
*
* @return
*/
@ParallelSupported
@IntermediateOp
@TerminalOpTriggered
S sorted();
// /**
// *
// * This method only runs sequentially, even in parallel stream and all elements will be loaded to memory.
// *
// * @return
// */
// @SequentialOnly
// S cached();
@ParallelSupported
@IntermediateOp
@TerminalOpTriggered
S reverseSorted();
/**
*
*
* This method only runs sequentially, even in parallel stream and retrieved elements will be saved in memory for next cycle.
*
* @return
*/
@SequentialOnly
@IntermediateOp
S cycled();
/**
*
*
* This method only runs sequentially, even in parallel stream and retrieved elements will be saved in memory for next cycle.
*
* @param rounds
*
* @return
*/
@SequentialOnly
@IntermediateOp
S cycled(long rounds);
/**
*
* This method only runs sequentially, even in parallel stream.
*
* @return
*/
@SequentialOnly
@IntermediateOp
Stream indexed();
/**
*
* @param n
* @return
*/
@SequentialOnly
@IntermediateOp
S skip(long n);
/**
*
* @param n
* @param consumer
* @return
*/
@Beta
@ParallelSupported
@IntermediateOp
S skip(long n, C consumer);
/**
*
* @param maxSize
* @return
*/
@SequentialOnly
@IntermediateOp
S limit(long maxSize);
// /**
// * Same as: {@code stream.skip(from).limit(to - from)}.
// *
// * @param from
// * @param to
// * @return
// * @deprecated
// */
// @Deprecated
// @SequentialOnly
// S slice(long from, long to);
@SequentialOnly
@IntermediateOp
S step(long step);
/**
* Same as {@code peek}
*
* @param action
* @return
* @see #peek(Object)
*/
@ParallelSupported
@IntermediateOp
S onEach(C action);
@ParallelSupported
@IntermediateOp
S peek(C action);
@SequentialOnly
@IntermediateOp
S prepend(S stream);
@SequentialOnly
@IntermediateOp
S prepend(OT op);
@SequentialOnly
@IntermediateOp
S append(S stream);
@SequentialOnly
@IntermediateOp
S append(OT op);
@SequentialOnly
@IntermediateOp
S appendIfEmpty(Supplier extends S> supplier);
@SequentialOnly
@IntermediateOp
S throwIfEmpty(Supplier extends RuntimeException> exceptionSupplier);
/**
* This is a terminal operation. That's to say this stream will be closed after this operation.
*
* @param
* @param
* @param func
* @return
* @throws E
*/
@TerminalOp
Optional applyIfNotEmpty(Throwables.Function super S, R, E> func) throws E;
/**
* This is a terminal operation. That's to say this stream will be closed after this operation.
*
* @param
* @param action
* @throws E
* @return
*/
@TerminalOp
OrElse acceptIfNotEmpty(Throwables.Consumer super S, E> action) throws E;
// /**
// *
// * This method only runs sequentially, even in parallel stream and all elements will be loaded to memory.
// *
// * @return
// */
// @SequentialOnly
// S cached();
@SequentialOnly
@TerminalOp
String join(CharSequence delimiter);
@SequentialOnly
@TerminalOp
String join(final CharSequence delimiter, final CharSequence prefix, final CharSequence suffix);
// /**
// * Same as: {@code stream.skip(from).limit(to - from)}.
// *
// * @param from
// * @param to
// * @return
// * @deprecated
// */
// @Deprecated
// @SequentialOnly
// S slice(long from, long to);
/**
*
* All elements will be loaded to memory and sorted if not yet.
*
* @return
*/
@SequentialOnly
@IntermediateOp
Optional