Hi folks, In this blog we would talk about one of the features of Java 8 which is Streams. The java package java.util.stream contains classes to support functional-style operations on streams i.e. map, filter etc. Streams is not a data structure that stores something instead of it a way to flow data from source to destination with some intermediate operation in between. Let’s take an example first so that we can grasp concepts fast.

This image has an empty alt attribute; its file name is untitled-diagram-15.png

Let’s say, we have a collection of books Collection where Book class has two attributes writer and price and we want to obtain the sum of price’s where the writer is “Neil Chetan”. In order to obtain the result, we need to do some like –

int totalPrice = collection.stream()
                      .filter(b -> b.getWriter() == "Neil chetan")
                      .map(b -> b.getPrice())
                      .sum();

The stream is a series of operations that can be Intermediate or terminal operation.

  • The Intermediate operations return a new stream as result and we can call multiple intermediate operations one after the other. The Intermediate operations are executed before terminal operation. These types of operation are always lazy in nature means they will not be executed until the result of a processing is needed.
  • The Terminal operation allows us to produce a final result from the remaining data. After applying this operation on the stream means that the stream has consumed. Computation on the stream pipeline is performed when the terminal operation is initiated.

#java #scala #tech blogs #java 8 streams #java8 #streams

Java-8 Streams Tutorial
1.35 GEEK