Intermediate operations are operations that do not produce a final result on their own but rather transform or filter the elements of a stream and return a new stream that can be further operated on. Some examples of intermediate operations are:
Map (): Transforms each stream element using a given function.
Filter (): Retains only the elements of a stream that satisfy a given condition.
Distinct (): Removes duplicates from a stream.
Sorted (): Sorts the elements of a stream based on a given ordering.
Limit (): Truncates a stream to a specified maximum size.
Skip (): Skips a set number of elements from the beginning of a stream.
Terminal operations are operations that produce a final result or side-effect and terminate the processing of a stream. Once a terminal operation is applied to a stream, the stream cannot be used again. Some examples of terminal operations are:
forEach(): Performs a given action for each stream element.
toArray(): Collects the elements of a stream into an array.
Reduce (): Combines the elements of a stream using a given function and returns a single result.
Collect (): Collects the elements of a stream into a collection or other data structure.
Count (): Counts the number of elements in a stream.
Min (), max(): Returns a stream's minimum or maximum element based on a given ordering.