Java Stream API has been introduced in Java 8 which majorly focuses on providing various method for operation on Source. It operates around Source as wrapper making bulk operation fast and in few lines.
Stream bring functional programming to Java and heavily uses lambda expression for its implementation. ParallelStream can be used for multi-threaded operations.
Stream operations can be classified into two sets
- Intermediate Operations
- Terminal Operations
Intermediate Operations will return Stream after completing its operation. We can say it as it converts from one stream to another stream after operating on it. This enables operations to be chained together. As an example, we have List of Strings as Source and we want apply filter and sort operations on it. It can be chained as below
.filter(x- > x.contains("a"))
If you notice that we cannot add any other operation after forEach which is an Terminal operation, so terminal operation has to be applied only end of pipeline.
Here is list of Intermediate Operations provided by Stream
Terminal operation will return non stream values which can be primitive, object, collection or may not return anything. This restrict operations to be pipelined. Hence it is used for end result only.
Here is the list of all Stream terminal operations: