Stream operations are 2 types, intermediate and terminal operations, and together they form stream pipelines.
Intermediate operations return new are eam which allows you to call multiple operations in a form of a query. This operation does not get executed until a terminal operation is invoked. All Intermediate operations are lazy, so they are not executed until a result of a processing is actually needed, that is, traversal of the Stream does not start until the terminal operation of the pipeline is executed.
Examples of Stream intermediate operations include filter(),
, map(), flatMap(), limit(), skip(), distinct(), sorted()peek().
Terminal operations are eagerly executed, and is always the last operation in a Stream pipeline. It can return a primitive type value (boolean, long), a concrete type (Optional value object), or nothing (void).
Examples of Terminal operations like Stream.forEach, IntStream.sum, may traverse the stream to produce a result or a side-effect. After the terminal operation is completed, the stream pipeline is considered consumed, and can no longer be used.