The java.util.stream.Stream interface provides a sequence of elements supporting sequential and parallel aggregate operations.
Java Stream Methods
The table below contains common 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 |
|---|---|
| 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. |
| toList() | Returns an immutable list containing the elements of this stream. |
| allMatch() | Returns whether all elements of this stream match the provided predicate. |
| anyMatch() | Returns whether any elements of this stream match the provided predicate. |
| 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. |
| 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 (according to Object.equals(Object)) 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 the 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, replacing each mapped stream’s elements with the elements of the final resulting stream. |
| 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. |
In conclusion, the java.util.stream.Stream interface provides a comprehensive set of methods to perform various operations on streams of objects. These methods offer flexibility in processing and manipulating stream elements effectively. For more detailed information, refer to the official Java Documentation.