Java
Java Stream API Methods
One-line reference for every method on java.util.stream.Stream, including BaseStream and Java 24 gather.
8 views
Intermediate operations
filter(Predicate)— keeps elements that match the predicate.map(Function)— transforms each element into another object.mapToInt(ToIntFunction)— maps each element to anint(IntStream).mapToLong(ToLongFunction)— maps each element to along(LongStream).mapToDouble(ToDoubleFunction)— maps each element to adouble(DoubleStream).flatMap(Function)— maps each element to a stream and flattens the results into one stream.flatMapToInt(Function)— flat-maps to anIntStream.flatMapToLong(Function)— flat-maps to aLongStream.flatMapToDouble(Function)— flat-maps to aDoubleStream.mapMulti(BiConsumer)— lets the mapper push zero or more results per element (Java 16; often cheaper thanflatMap).mapMultiToInt(BiConsumer)—mapMultithat emitsints.mapMultiToLong(BiConsumer)—mapMultithat emitslongs.mapMultiToDouble(BiConsumer)—mapMultithat emitsdoubles.distinct()— drops duplicates usingequals.sorted()— sorts in natural order.sorted(Comparator)— sorts with the given comparator.peek(Consumer)— runs an action on each element as it flows through (for debugging; do not rely on it for side effects).limit(long)— keeps at most the first n elements.skip(long)— discards the first n elements.takeWhile(Predicate)— keeps the prefix while the predicate holds (Java 9).dropWhile(Predicate)— drops the prefix while the predicate holds, then keeps the rest (Java 9).gather(Gatherer)— applies a custom intermediate operation (Java 24); built-ins live onGatherers(windowFixed,windowSliding,fold,scan,mapConcurrent).
Terminal operations
forEach(Consumer)— runs an action on each element; order is not guaranteed in parallel.forEachOrdered(Consumer)— same, but respects encounter order.toArray()— collects elements into anObject[].toArray(IntFunction)— collects into a typed array, e.g.String[]::new.reduce(BinaryOperator)— folds elements into anOptionalwith no identity.reduce(identity, BinaryOperator)— folds elements starting from an identity value.reduce(identity, BiFunction, BinaryOperator)— maps and folds in one reduction (useful in parallel).collect(Collector)— mutable reduction via a collector (toList,groupingBy,joining, …).collect(Supplier, BiConsumer, BiConsumer)— mutable reduction with an explicit supplier, accumulator, and combiner.toList()— collects into an unmodifiableList(Java 16).min(Comparator)— returns the smallest element as anOptional.max(Comparator)— returns the largest element as anOptional.count()— returns the number of elements.anyMatch(Predicate)— true if any element matches; may short-circuit.allMatch(Predicate)— true if every element matches (vacuously true if empty).noneMatch(Predicate)— true if no element matches.findFirst()— first element in encounter order, as anOptional.findAny()— any element, as anOptional(cheaper in parallel).
Static factories
empty()— an empty sequential stream.of(T)— a stream of one element.of(T...)— a stream of the given elements.ofNullable(T)— a stream of the value, or empty if it is null (Java 9).iterate(seed, UnaryOperator)— infinite sequence: seed, f(seed), f(f(seed)), …iterate(seed, Predicate, UnaryOperator)— same, but stops when the predicate fails (Java 9).generate(Supplier)— infinite stream from a supplier.concat(Stream, Stream)— lazy concatenation of two streams.builder()— a mutableStream.Builderfor assembling a stream.
From BaseStream (also on Stream)
iterator()— anIteratorover the remaining elements.spliterator()— aSpliteratorover the remaining elements.isParallel()— whether the pipeline will run in parallel.sequential()— switches the pipeline to sequential.parallel()— switches the pipeline to parallel.unordered()— drops the encounter-order constraint so ops can be faster.onClose(Runnable)— registers a handler to run when the stream is closed.close()— closes the stream and runs close handlers (AutoCloseable; needed mainly for I/O-backed streams such asFiles.lines).