Explain Java 8 Streams API | What is Filter, Map, FlatMap ?| Example Java Code Demo | InterviewDOT
Channel: Interview DOT
Date: 2020-03-06

Click here - https://www.youtube.com/channel/UCd0U_xlQxdZynq09knDszXA?sub_confirmation=1 to get notifications.

Tamil Explain Java 8 Streams API | What is Filter, Map, FlatMap ?| Example Java Code Demo | InterviewDOT

The Java 8 release of Java Programming language was a game-changer version. It not only provided some useful method but totally changed the way you write programs in Java. The most significant change it brings in the mindset of Java developers was to think functional and supported that by providing critical features like lambda expression and Stream API, which takes advantage of parallel processing and functional operations like filter, map, flatMap, etc. Since then, a lot of Java developers are trying their hands to learn those significant changes like lambda expression, method reference, new Date and Time classes, and, more importantly, Stream API for bulk data operations.

How to use Streams in Java 8
You can use Streams to do a lot of things in Java 8. By the way, this stream is a bit different than your Java IO streams, e.g. InputStream and OutputStream. This stream provides an elegant lazy evaluation of an expression, and it also supports intermediate and terminal operations.

Terminal operations are used to produce a result, and after that, you cannot reuse them.

The good thing about Streams is that they leave source collection intact, i.e. operations on streams don't affect the collection from which streams are obtained. By the way, you can get Stream not just from the Collection but from other sources like Random Number generator and FileInputStream.

Java-8 Stream terminal operations produces a non-stream, result such as primitive value, a collection or no value at all. Terminal operations are typically preceded by intermediate operations which return another Stream which allows operations to be connected in a form of a query.

How does Stream API work in Java?
Introduced in Java 8, the Stream API is used to process collections of objects. Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result.


Stream
Stream is a sequence of elements supporting sequential and parallel aggregate operations.

Stream is not a data type or a collection by itself. Sequence of elements are created out of a source, like a collection object, and transformed using different operations connected as pipelines and ends with a final operation.

Java Streams Operations
As stated above, Java stream operation are of two types intermediate and terminal. The intermediate operations are classified as stateful or stateless based on their ability to process elements independently.

If each of the element can be processed independently without retaining any information for processing other elements, then they are stateless. If any information is retained for processing of other elements, then they are stateful.

Terminal Operations
Terminal operations consumes the stream pipeline. It does not produce another stream like intermediate operations. It produces a result or a side-effect.

Benefits of Stream API ?
Streams provide the most convenient and natural way to apply functions to sequences of objects. Streams encourage less mutability. This is sort of related to the functional programming aspect -- the kind of programs you write using streams tend to be the kind of programs where you don't modify objects