Java Stream Methods

The Stream interface in Java provides a powerful way to process sequences of elements in a declarative manner. It is part of the Java Stream API, which allows developers to perform functional-style operations on streams of elements, such as filtering, mapping, and reducing.

This guide covers various methods available in the Stream interface. Each method is described in simple terms to help beginners understand how to use them. We use Lambda expression to implement all these Stream interface methods.

For more detailed information, please refer to the official Java SE Documentation.

Java Stream Interface Methods

The table below contains various methods of the Java Stream interface, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
allMatch() Returns whether all elements of this stream match the provided predicate.
anyMatch() Returns whether any elements of this stream match the provided predicate.
collect() Performs a mutable reduction operation on the elements of this stream using a Collector.
count() Returns the count of elements in this stream.
distinct() Returns a stream consisting of the distinct elements of this stream.
empty() Returns an empty sequential Stream.
filter() Returns a stream consisting of the elements of this stream that match the given predicate.
findAny() Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
findFirst() Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty.
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.
flatMapToDouble() Returns a 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.
flatMapToInt() 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.
forEach() Performs an action for each element of this stream.
generate() Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.
limit() Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
mapMulti() 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.
mapToDouble() Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.
mapToInt() Returns an IntStream consisting of the results of applying the given function to the elements of this stream.
max() Returns the maximum element of this stream according to the provided Comparator.
min() Returns the minimum element of this stream according to the provided Comparator.
noneMatch() Returns whether no elements of this stream match the provided predicate.
of() Returns a sequential ordered stream whose elements are the specified values.
ofNullable() Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
peek() 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.
reduce() Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
skip() Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
sorted() Returns a stream consisting of the elements of this stream, sorted according to natural order.
toArray() Returns an array containing the elements of this stream.
toList() Returns an unmodifiable List containing the elements of this stream.

References:

Comments