Java: Beginner Guide to Stream API

a thread...

Streams are a way to perform operations on the whole collection of data.

Stream never modifies the source data, it processes it and gives us the result without modifying the original data.
Let's see few popular ways to create stream:

1. Collection. stream()
2. Stream.of(T... values)
3. Arrays. stream()
4. Stream.builder()

Eg:️
Consider stream as a pipeline, where we perform mainly two different operations on the source data:

1. Intermediate Operations(IO)
2. Terminal Operations(TO)

Here's what the stream pipeline looks like:

Source -> Intermediate Operations -> Terminal Operations
Intermediate Operations(IO):

It returns another stream object, after this, we can either call another IO or TO.

Few methods in Stream API for Intermediate operations:
1. filter()
2. map()
3. sorted()

and many more...
Examples:

filter() - filters out the data based on any boolean condition - below we're filtering elements starting with the letter a.

map() - performs some given operation on the whole data set - below we're converting every element to upper case.
Terminal Operations(TO):

It's the last operation done on stream that's why it's called terminal.

This operation returns the final result.

Few methods in Stream API for Terminal operations:
1. collect()
2. count()
3. forEach()

and many more...
Examples:

collect() - collects the elements in the given collection as shown below in the first case it is returning data in the form of a list.

count() - counts the no of the element in the stream and returns in the form of long.
Remember:

A Single pipeline can have multiple Intermediate Operations.

And only a single Terminal Operations.

Eg:
Below, we've used two Intermediate Operations i.e. filter() and map() but only used single Terminal Operation i.e. count().

More from Vikas Rajput

You May Also Like

Still wondering about this 🤔


save as q
ARE WE FAMILIAR WITH THE MEANING & POWER OF MANTRAS WE CHANT?

Whenever we chant a Mantra in Sanskrit, it starts with 'Om' and mostly ends with 'Swaha' or 'Namaha'. This specific alignment of words has a specific meaning to it which is explained in Dharma Shastra.


Mantra is a Sanskrit word meaning sacred syllable or sacred word. But Mantras r not just words put together,they r also vibrations.The whole Universe is a cosmic energy in different states of vibration &this energy in different states of vibration forms the objects of Universe.

According to Scriptures,Om is considered to be ekaakshar Brahman,which means Om is the ruler of 3 properties of creator,preserver&destroyer which make the
https://t.co/lyhkWeCdtv is also seen as a symbol of Lord Ganesha, as when starting the prayer,it's him who is worshipped 1st.


'Om' is the sound of the Universe. It's the first original vibration of the nothingness through which manifested the whole Cosmos. It represents the birth, death and rebirth process. Chanting 'Om' brings us into harmonic resonance with the Universe. It is a scientific fact.

Therefore, Mantras are described as vibrational words that are recited, spoken or sung and are invoked towards attaining some very specific results. They make very specific sounds at a frequency that conveys a directive into our subconcious.