Class NonEmptyStream<T>
- Type Parameters:
T- the type of the stream elements
- All Implemented Interfaces:
AutoCloseable,BaseStream<T,,Stream<T>> Stream<T>
Stream API for processing the stream knowing
it is non-empty, including, but not limited to:
first()andany()limitToNonEmpty(long)collect(EmptyResultIfEmptySourceCollector)reduceFromFirst(BinaryOperator)
NonEmptyStream,
such as:
This selection may be extended in the future. For operations which can not guarantie the
result will still be a non-empty stream, such as filter and limit, will yield a
regular Stream.-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T extends Object> -
Method Summary
Modifier and TypeMethodDescriptionbooleanany()Return any element from the stream.booleanvoidclose()<R> Rcollect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) <R,A> R <A,R> R collect(EmptyResultIfEmptySourceCollector<? super T, A, R> collector) Collect the stream elements by using aEmptyResultIfEmptySourceCollector.static <T> NonEmptyStream<T>concat(Stream<? extends T> a, NonEmptyStream<? extends T> b) Create a stream by concatenating a regularStreamfollowed by aNonEmptyStream, in the same manner asStream.concat(Stream, Stream).static <T> NonEmptyStream<T>concat(NonEmptyStream<? extends T> a, Stream<? extends T> b) Create a stream by concatenating aNonEmptyStreamfollowed by a regularStream, in the same manner asStream.concat(Stream, Stream).static <T> NonEmptyStream<T>concat(NonEmptyStream<? extends T> a, NonEmptyStream<? extends T> b) Create a stream by concatenating two non-empty streams, in the same manner asStream.concat(Stream, Stream).longcount()distinct()findAny()first()Return the first element of the stream.<R> Stream<R><R> NonEmptyStream<R>flatMap(ToNonEmptyStreamFunction<? super T, ? extends R> mapper) Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper) flatMapToInt(Function<? super T, ? extends IntStream> mapper) flatMapToLong(Function<? super T, ? extends LongStream> mapper) voidvoidforEachOrdered(Consumer<? super T> action) static <T> NonEmptyStream<T>Create the same stream as produced byStream.generate(Supplier), but typed asNonEmptyStream.booleanstatic <T> NonEmptyStream<T>iterate(T seed, UnaryOperator<T> f) Create the same stream as produced byStream.iterate(Object, UnaryOperator), but typed asNonEmptyStream.iterator()limit(long maxSize) limitToNonEmpty(long maxSizeMoreThanZero) Returns a stream consisting of the elements of this stream, truncated to be no longer thanmaxSizeMoreThanZeroin length.<R> NonEmptyStream<R>mapToDouble(ToDoubleFunction<? super T> mapper) mapToInt(ToIntFunction<? super T> mapper) mapToLong(ToLongFunction<? super T> mapper) max(Comparator<? super T> comparator) min(Comparator<? super T> comparator) booleanstatic <T> NonEmptyStream<T>Create a non-empty stream where the first element is resolved from aSupplier, and remaining elements are provided from another stream.static <T> NonEmptyStream<T>of(T singleElement) Create a non-empty stream containing a single element.static <T> NonEmptyStream<T>Create a non-empty stream whose elements are a given first value, and remaining elements are provided from another stream.static <T> NonEmptyStream<T>of(T firstElement, T... remainingElements) Create a non-empty stream whose elements are the specified values.parallel()reduce(BinaryOperator<T> accumulator) reduce(T identity, BinaryOperator<T> accumulator) <U> Ureduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) reduceFromFirst(BinaryOperator<T> accumulator) Performs a reduction on the elements of this stream, using the first element of the stream (guarantied to be available) and an associative accumulation function, and returns the reduced value.skip(long n) sorted()sorted(Comparator<? super T> comparator) Object[]toArray()<A> A[]toArray(IntFunction<A[]> generator) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.stream.Stream
dropWhile, mapMulti, mapMultiToDouble, mapMultiToInt, mapMultiToLong, takeWhile, toList
-
Method Details
-
of
Create a non-empty stream containing a single element.- Type Parameters:
T- the type of the single element in the stream- Parameters:
singleElement- the element- Returns:
- the new singleton non-empty stream
-
of
Create a non-empty stream whose elements are the specified values.- Type Parameters:
T- the type of stream elements- Parameters:
firstElement- the first elementremainingElements- the remaining elements after the first- Returns:
- the new non-empty stream
-
of
Create a non-empty stream whose elements are a given first value, and remaining elements are provided from another stream.- Type Parameters:
T- the type of stream elements- Parameters:
firstElement- the first elementremainingElements- the remaining elements after the first- Returns:
- the new non-empty stream
-
of
public static <T> NonEmptyStream<T> of(Supplier<? extends T> firstElement, Stream<T> remainingElements) Create a non-empty stream where the first element is resolved from aSupplier, and remaining elements are provided from another stream.- Type Parameters:
T- the type of stream elements- Parameters:
firstElement- the supplier of the first elementremainingElements- the remaining elements after the first- Returns:
- the new non-empty stream
-
concat
Create a stream by concatenating aNonEmptyStreamfollowed by a regularStream, in the same manner asStream.concat(Stream, Stream). The resulting stream is also non-empty.- Type Parameters:
T- The type of stream elements- Parameters:
a- the first stream, non-emptyb- the second stream- Returns:
- the concatenation of the two input streams
- See Also:
-
concat
Create a stream by concatenating a regularStreamfollowed by aNonEmptyStream, in the same manner asStream.concat(Stream, Stream). The resulting stream is also non-empty.- Type Parameters:
T- The type of stream elements- Parameters:
a- the first streamb- the second stream, non-empty- Returns:
- the concatenation of the two input streams
- See Also:
-
concat
public static <T> NonEmptyStream<T> concat(NonEmptyStream<? extends T> a, NonEmptyStream<? extends T> b) Create a stream by concatenating two non-empty streams, in the same manner asStream.concat(Stream, Stream). The resulting stream is also non-empty.This method overload is needed to avoid ambiguity with
concat(NonEmptyStream, Stream)andconcat(Stream, NonEmptyStream)when concatenating two non-empty streams.- Type Parameters:
T- The type of stream elements- Parameters:
a- the first non-empty streamb- the second non-empty stream- Returns:
- the concatenation of the two input streams
- See Also:
-
iterate
Create the same stream as produced byStream.iterate(Object, UnaryOperator), but typed asNonEmptyStream.- Type Parameters:
T- the type of stream elements- Parameters:
seed- the initial elementf- a function to be applied to to the previous element to produce a new element- Returns:
- the new infinite non-empty stream
- See Also:
-
generate
Create the same stream as produced byStream.generate(Supplier), but typed asNonEmptyStream.- Type Parameters:
T- the type of stream elements- Parameters:
s- theSupplierof generated elements- Returns:
- a new infinite non-empty stream
- See Also:
-
collect
public <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) -
collect
-
collect
Collect the stream elements by using aEmptyResultIfEmptySourceCollector.This is an extension to the general
StreamAPI, as a non-empty stream can always produce a value without needing a provided initial "identity" value.- Parameters:
collector- theCollectordescribing the reduction- Returns:
- the result from collecting the elements
- See Also:
-
map
-
iterator
- Specified by:
iteratorin interfaceBaseStream<T,Stream<T>>
-
spliterator
- Specified by:
spliteratorin interfaceBaseStream<T,Stream<T>>
-
isParallel
public boolean isParallel()- Specified by:
isParallelin interfaceBaseStream<T,Stream<T>>
-
sequential
- Specified by:
sequentialin interfaceBaseStream<T,Stream<T>>
-
parallel
- Specified by:
parallelin interfaceBaseStream<T,Stream<T>>
-
unordered
- Specified by:
unorderedin interfaceBaseStream<T,Stream<T>>
-
onClose
- Specified by:
onClosein interfaceBaseStream<T,Stream<T>>
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBaseStream<T,Stream<T>>
-
filter
-
mapToInt
-
mapToLong
-
mapToDouble
- Specified by:
mapToDoublein interfaceStream<T>
-
flatMap
-
flatMap
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.This is an extension to the general
StreamAPI, as flat-mapping to non-empty streams will preserve the non-empty guarantee of the stream.- Type Parameters:
R- The element type of the new stream- Parameters:
mapper- a non-interfering, stateless function to apply to each element which produces a stream of new values- Returns:
- the new non-empty stream
-
flatMapToInt
- Specified by:
flatMapToIntin interfaceStream<T>
-
flatMapToLong
- Specified by:
flatMapToLongin interfaceStream<T>
-
flatMapToDouble
- Specified by:
flatMapToDoublein interfaceStream<T>
-
distinct
-
sorted
-
sorted
-
peek
-
limitToNonEmpty
Returns a stream consisting of the elements of this stream, truncated to be no longer thanmaxSizeMoreThanZeroin length.- Parameters:
maxSizeMoreThanZero- the number of elements the stream should be limited to, must be 1 or greater (>= 1)- Returns:
- the new non-empty stream
- Throws:
IllegalArgumentException- if the givenmaxSizeMoreThanZerois 0 or less- See Also:
-
limit
-
skip
-
forEach
-
forEachOrdered
- Specified by:
forEachOrderedin interfaceStream<T>
-
toArray
-
toArray
-
reduce
-
reduceFromFirst
Performs a reduction on the elements of this stream, using the first element of the stream (guarantied to be available) and an associative accumulation function, and returns the reduced value.This is an extension to the general
StreamAPI, as a non-empty stream can always produce a value without needing a provided initial "identity" value.- Parameters:
accumulator- an associative, non-interfering, stateless function for combining two values- Returns:
- the result of the reduction
- See Also:
-
reduce
-
reduce
-
min
-
max
-
count
public long count() -
anyMatch
-
allMatch
-
noneMatch
-
first
Return the first element of the stream.This is an extension to the general
StreamAPI, as a non-empty stream is guarantied to always have at least one value.- Returns:
- the first value of the stream
- See Also:
-
findFirst
As this is a non-empty stream, this method always returns a non-empty
Optionalwith the first value of the stream. -
any
Return any element from the stream.This is an extension to the general
StreamAPI, as a non-empty stream is guarantied to always have at least one value.- Returns:
- any value from the stream
- See Also:
-
findAny
As this is a non-empty stream, this method always returns a non-empty
Optionalwith the any value from the stream.
-