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

My top 10 tweets of the year

A thread 👇

https://t.co/xj4js6shhy


https://t.co/b81zoW6u1d


https://t.co/1147it02zs


https://t.co/A7XCU5fC2m
1

From today, we will memorize the names of 27 Nakshatras in Vedic Jyotish to never forget in life.

I will write 4 names. Repeat them in SAME sequence twice in morning, noon, evening. Each day, revise new names + recall all previously learnt names.

Pls RT if you are in.

2

Today's Nakshatras are:-

1. Ashwini - अश्विनी

2. Bharani - भरणी

3. Krittika - कृत्तिका

4. Rohini - रोहिणी

Ashwini - अश्विनी is the FIRST Nakshatra.

Repeat these names TWICE now, tomorrow morning, noon and evening. Like this tweet if you have revised 8 times as told.

3

Today's Nakshatras are:-

5. Mrigashira - मृगशिरा

6. Ardra - आर्द्रा

7. Punarvasu - पुनर्वसु

8. Pushya - पुष्य

First recall previously learnt Nakshatras twice. Then recite these TWICE now, tomorrow morning, noon & evening in SAME order. Like this tweet only after doing so.

4

Today's Nakshatras are:-

9. Ashlesha - अश्लेषा

10. Magha - मघा

11. Purvaphalguni - पूर्वाफाल्गुनी

12. Uttaraphalguni - उत्तराफाल्गुनी

Purva means that comes before (P se Purva, P se pehele), and Uttara comes later.

Read next tweet too.

5

Purva, Uttara prefixes come in other Nakshatras too. Purva= pehele wala. Remember.

First recall previously learnt 8 Nakshatras twice. Then recite those in Tweet #4 TWICE now, tomorrow morning, noon & evening in SAME order. Like this tweet if you have read Tweets #4 & 5, both.
**Thread on Bravery of Sikhs**
(I am forced to do this due to continuous hounding of Sikh Extremists since yesterday)

Rani Jindan Kaur, wife of Maharaja Ranjit Singh had illegitimate relations with Lal Singh (PM of Ranjit Singh). Along with Lal Singh, she attacked Jammu, burnt - https://t.co/EfjAq59AyI


Hindu villages of Jasrota, caused rebellion in Jammu, attacked Kishtwar.

Ancestors of Raja Ranjit Singh, The Sansi Tribe used to give daughters as concubines to Jahangir.


The Ludhiana Political Agency (Later NW Fronties Prov) was formed by less than 4000 British soldiers who advanced from Delhi and reached Ludhiana, receiving submissions of all sikh chiefs along the way. The submission of the troops of Raja of Lahore (Ranjit Singh) at Ambala.

Dabistan a contemporary book on Sikh History tells us that Guru Hargobind broke Naina devi Idol Same source describes Guru Hargobind serving a eunuch
YarKhan. (ref was proudly shared by a sikh on twitter)
Gobind Singh followed Bahadur Shah to Deccan to fight for him.


In Zafarnama, Guru Gobind Singh states that the reason he was in conflict with the Hill Rajas was that while they were worshiping idols, while he was an idol-breaker.

And idiot Hindus place him along Maharana, Prithviraj and Shivaji as saviours of Dharma.