1. Obtain Stream
Streams can be obtained in a number of ways. Some examples include:
- From a Collection via the stream() and parallelStream() methods;
- From an array via Arrays.stream(Object[]);
- From static factory methods on the stream classes, such as Stream.of(Object[]), IntStream.range(int, int) or Stream.iterate(Object, UnaryOperator);
- The lines of a file can be obtained from BufferedReader.lines();
- Streams of file paths can be obtained from methods in Files;
- Streams of random numbers can be obtained from Random.ints();
- Numerous other stream-bearing methods in the JDK, including BitSet.stream(), Pattern.splitAsStream(java.lang.CharSequence), and JarFile.stream().
2. API
2.1. terminal operation
-
R collect(Supplier supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner) ??
Performs a mutable reduction operation on the elements of this stream. -
<R,A> R collect(Collector<? super T,A,R> collector)
Performs a mutable reduction operation on the elements of this stream using a Collector.
2.1.1. short-circuiting terminal operation
-
boolean allMatch(Predicate<? super T> predicate)
Returns true if either all elements of the stream match the provided predicate or the stream is empty, otherwise false. -
boolean anyMatch(Predicate<? super T> predicate)
Returns true if any elements of the stream match the provided predicate, otherwise false.
2.2. intermediate operation
static
Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.
long count()
Returns the count of elements in this stream.
Stream
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.
default Stream
Returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
static
Returns an empty sequential Stream.
Stream
Returns a stream consisting of the elements of this stream that match the given predicate.
Optional
Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
Optional
Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty.
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.
DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
Returns an DoubleStream 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.
IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper)
Returns an IntStream 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.
LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper)
Returns an LongStream 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.
void forEach(Consumer<? super T> action)
Performs an action for each element of this stream.
void forEachOrdered(Consumer<? super T> action)
Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
static
Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.
static
Returns a sequential ordered Stream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.
static
Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
Stream
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
Returns a stream consisting of the results of applying the given function to the elements of this stream.
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.
IntStream mapToInt(ToIntFunction<? super T> mapper)
Returns an IntStream consisting of the results of applying the given function to the elements of this stream.
LongStream mapToLong(ToLongFunction<? super T> mapper)
Returns a LongStream consisting of the results of applying the given function to the elements of this stream.
Optional
Returns the maximum element of this stream according to the provided Comparator.
Optional
Returns the minimum element of this stream according to the provided Comparator.
boolean noneMatch(Predicate<? super T> predicate)
Returns whether no elements of this stream match the provided predicate.
static
Returns a sequential Stream containing a single element.
static
Returns a sequential ordered stream whose elements are the specified values.
static
Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
Stream
Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
Optional
Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
T reduce(T identity, BinaryOperator
Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator combiner)
Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions.
Stream
Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
Stream
Returns a stream consisting of the elements of this stream, sorted according to natural order.
Stream
Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
default Stream
Returns, if this stream is ordered, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
Object[] toArray()
Returns an array containing the elements of this stream.
A[] toArray(IntFunction<A[]> generator)
Returns an array containing the elements of this stream, using the provided generator function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.
3. Collectors
averagingDouble
averagingInt
averagingLong
boxSupplier
castingIdentity
collectingAndThen
Collectors
computeFinalSum
counting
groupingBy
groupingByConcurrent
joining
mapMerger
mapping
maxBy
minBy
partitioningBy
reducing
summarizingDouble
summarizingInt
summarizingLong
summingDouble
summingInt
summingLong
sumWithCompensation
throwingMerger
toCollection
toConcurrentMap
toList
toMap
toSet