Streams are introduced in java 8. It is defined in java.util.stream package. A stream is sequence of objects which supports various methods. That can be pipelined to produce the result.
Features of Stream
- Stream is not a data structure. It takes input from various sources (like array, collection, data structure) performs an operation and gives output.
- It does not store element.
- It does not change the original data.
- The elements of streams can be visited only once like an iterator, if you want to view them again then you have to create a new stream.
- It support different operations like filter, map, reduce, limit etc.
A stream can be composed of multiple operations/functions that create a pipeline of data that flows. That is, the original data structure doesn’t change.
The stream is a pipeline of functions or operations. These operations can be classified as intermediate operations or terminal operations.
The difference between these two is in the output-
- If an operation results into another stream, to which you can apply another operation, then that operation is called as intermediate operation.
- If an operation results into a concrete type, it is called terminal operation.
Intermediate operations return stream which allows you to call multiple operations in a form of a query. Intermediate operations execute after terminal operations are invoked. Intermediate operations are lazy operations.
Lets look at some popular intermediate operations used in stream:-
- filter: The filter operation returns a stream of elements that satisfy the predicate passed in as a parameter to the operation.
List names = Arrays.asList("John","Sam","Jordan","George").stream().filter(s->s.startsWith("S")).collect(Collectors.toList());
In the above example the stream will perform the filter operation on list and gives the sorted list in the end.
- map: The map operation returns a stream of the element after they have processed by the function passed in as a parameter.
List number = Arrays.asList(2,3,4,5).stream().map(s->s+s).collect(Collectors.toList());
In the above line of code, the map will perform an operation which is passed to it that is addition like 2+2=4, 3+3=6.
- sorted : The sorted method is used to sort the stream.
List num = Arrays.asList(7,5,9,2).stream().sorted().collect(Collectors.toList());
In above example, stream will perform the sort operation and gives the list in sorted order.
Terminal operations are preceded by intermediate operations which return another stream which allows operations to be connected in a form of a query.
- collect : The Stream.collect terminal operation will convert the stream into some other container such as a list.
Set stringSet = Stream.of("hello","world").collect(Collectors.toSet());
In the above example, it will convert the stream into set.
- foreach: The Stream.forEach method will perform an action for each element in the stream. It is a simplified inline way to write a for loop.
Stream.of("Hello", "World").forEach(str -> System.out.println(str));
In above example, foreach is used to iterate, it will go through each element.
- reduce: Stream.reduce operations are commonly found in statistic operations such as long summary statistics and combine the stream elements into one using a Binary Operator.
int sum = IntStream.of(1, 2, 3, 4).reduce(0, (a, b) -> a + b);
In above example, stream will be reduced into int.