Welcome to Java4u

A Single Place for all Java Resources

Looking for something?

Subscribe to this blog!

Receive the latest posts by email.

.Just enter your email below if you want to subscribe!

Email

Friday, June 13, 2014

Java 8 : Streams




Streams can be defined as a sequence of elements from a source that supports aggregate operations on them.
The source here refers to a Collection or Arrays who provides data to a Stream. Stream keeps the order of the data as it is in the source. And aggregate operations or bulk operations are operations which allow us to express common manipulations on those values easily and clearly 
I.Streams vs. Collections :

All of us have watch online videos on YouTube or some other such website. When you start watching video, a small portion of file is first loaded into your computer and start playing. You don’t need to download complete video before start playing it. This is called streaming. I will try to relate this concept with respect to collections and differentiate with Streams.
At the basic level, the difference between Collections and Streams has to do with when things are computed. A Collection is an in-memory data structure, which holds all the values that the data structure currently has—every element in the Collection has to be computed before it can be added to the Collection. A Stream is a conceptually fixed data structure, in which elements are computed on demand. This gives rise to significant programming benefits. The idea is that a user will extract only the values they require from a Stream and these elements are only produced—invisibly to the user—as and when required. This is a form of a producer-consumer relationship.
In java, java.util.Stream represents a stream on which one or more operations can be performed. Stream operations are either intermediate or terminal. While terminal operations return a result of a certain type, intermediate operations return the stream itself so you can chain multiple method calls in a row. Streams are created on a source, e.g. a java.util.Collection like lists or sets (maps are not supported). Stream operations can either be executed sequential or parallel.
Based on above points, if we list down the various characteristics of Stream, they will be as follows:

Not a data structure
Designed for lambdas
Do not support indexed access
Can easily be outputted as arrays or lists
Lazy access supported 
Parallelizable



        I.            Converting streams to collections

 Please not this is not conversion. It’s just collecting the elements in stream into a collection or array.

        I.Core stream operations


Stream abstraction has a long list of useful functions for you. I am not going to cover them all, but I plan here to list down all most important ones, which you must know first hand.

Before moving ahead, lets build a collection of String beforehand. We will build out example on this list, so that it is easy to relate and understand.



List memberNames = new ArrayList<>();

memberNames.add("Amitabh");

memberNames.add("Shekhar");

memberNames.add("Aman");

memberNames.add("Rahul");

memberNames.add("Shahrukh");

memberNames.add("Salman");

memberNames.add("Yana");

memberNames.add("Lokesh");


0 comments: