Java Stream API : What is a Short Circuiting Stateful Intermediate Operation ? | Java 8 sort() | EP3

preview_player
Показать описание
In this video we will learn about, What is a Short Circuiting Stateful Intermediate Operation in Java 8 Stream API ? We will explore the difference between stateful and stateless intermediate operations. We will look into short circuiting methods like limit(), takeWhile() and, we will debug the same.

In our stream api tutorials so far, we will understood about one of the Java 8's new features called Stream API.

The java stream api introduced in java 8, changed the way we write and read java code.

Java Stream API is an absolute beast when it comes to data processing as it enables the scope of processing the data in a declarative approach. It opens a way to fusion multiple methods by doing method chaining and gives us a way to write lambda expressions as most of the Java streams methods accepts lambda as a parameter. Thus we can reduce our traditional loops/condition checks / conversion code and significantly improve our code readability.

In this Java Stream api tutorial, We will discuss about the followings :
1. Stream API in Java 8
2. sort() in Java Stream
3. Stateful Operations : Order of Execution
4. Stateful vs Stateless - Java doc
5. limit() as a short circuiting stateful intermediate operation
6. skip() in java 8
7. distinct() in java 8
8. takeWhile() in java 8

TIMESTAMP
------------------------
00:00 - Stream API in Java 8 recap
04:35 - sorting in java
09:56 - What is a stateful operation in Java 8 Stream ?
20:25 - How Java 8 Sorted works under the hood ?
24:53 - infinite Stream - The Iterate() method
34:27 - A stream api interview question
40:39 - Stream API : bringing in takeWhile()
44:25 - What is a short circuiting operation in Stream API
51:31 - skip() in Java 8 Stream
53:32 - distinct() in Java 8 Stream API
54:57 - A thought on takeWhile()
----------
The Story So far :

As we have discussed, The java Stream api is not a data structure but it is used to process a collection of objects.

There are two kind of operations used in Stream API.

One type of operation is called Intermediate operation. Intermediate operations are used to perform operations like filtration of elements , element transformation to another type, limiting data in a stream pipeline etc. After performing any intermediate operation over a stream, we again get a Stream Object back.

One point to note here is , the Streams in java are lazy. They do nothing until a terminal operation invoked over them. So terminal operations are going to trigger or start a stream , helps the stream to start processing the data in the pipeline.

Methods like filter(),map(),limit() are called intermediate operations, where as the methods like forEach(),collect(),count() are called terminal operations.

Another key point to remember is , any intermediate operations that we perform over a stream doesn't modify the source stream object. So streams in java are immutable and any intermediate operations performed over a stream object returns a new stream.

Here one thing to keep in mind is , Once we done consuming data from a stream, the stream pipeline considered as consumed and wont be reusable again.
--------------
As per the java doc the intermediate operations are further divided into two things. One thing is called Stateful operation and another is stateless intermediate functions.

One more thing is few operations are short circuiting in nature.

In this java 8 stream api foundation tutorial, we will discuss everything about it in detail.

For more free/paid courses visit

LINKS AND FREE LEARNING RESOURCES
========
Watch my new mock interview series for java developers

Watch my new Hello spring boot series

Watch Spring boot Hot topics

spring core

spring mvc

spring mvc intermediate

Spring JDBC

spring live project

SPRING SECURITY COURSE[NEW]

For more courses visit

Any time you are getting stuck with issues, Feel free to ask for support.

you can ping me on my Facebook page

Make sure to join my private Facebook page (Ask me here)
“SeleniumExpress - Support"

you can ask for support in my website forum

Follow me on Insta
Рекомендации по теме
Комментарии
Автор

Expertly and clearly presented third part to the streams API.

phoboslabsreviews
Автор

superb abhilash, i am getting more knowledge from stream api and i am waiting for parallel stream concept

premkumar-jzdi
Автор

this is what I was looking for, interals of stream

vinaykaushikinvisble
Автор

When i watch your videos it looks like watching web series. I enjoyed a lot.

tabishrizwan
Автор

"Great job, Abilash! Your videos are incredibly informative and valuable. Despite having over a decade of experience in the Java world, I always find your content engaging and learn something new. Keep up the excellent work!"

meenaradhakrishnan
Автор

Excellent explanation, thanks for sharing ♥

shankarn
Автор

This way of explanation is very unique, You really makes me understand Stream API's in much deeper way. Thanks a lot for this. I think no one else has explain this topic like this before😍

rahulbhatanagar
Автор

Thank you so much for such a detailed explanation. Waiting for next episode.

Even though there are many views now, I still see only few likes for such a good content. This content deserves more like ❤❤.

sunithm
Автор

Due to your videos, my level i getting increased and will keep on increasing just like generate() method until you don't apply limit() to it. Kindly create videos for Multithreading. i know you will do miracles in that topic as well . I have never seen guru like you. Charan sparsh by heart.

suruchiranjana
Автор

Just watched 2nd video of this series .... and here I got notification of 3rd . Enjoying

bihariiiiii
Автор

Wow… very insightful… quality content … instructor is a real teacher ….. channel name should be Java express…

Nagesh-n-Bhosle
Автор

@SeleniumExpress Seems the 37:55 answers are wrong. It will print upto 19 in reverse order

santhoshchandran
Автор

Hi @Abhilash
Can you tell me the scenario where we should use the takeWhile() function. bcz if the condition is not matched it terminates there. If other input may or may not be matched, then we lost the expected output.

chaitucherry
Автор

44:00 takeWhile() is part of Java 9 version. 🙂

DevDarshan-rbpl
Автор

When will have video on Parellel stream?

archananarawade
Автор

Output of second program, 19, 18, ...0, so option is none of the above 😅 is it correct ?

roopant
Автор

Hi, when we have peek, why do we need foreach?

keerthikeerthi
Автор

Hi Abhilash,
Thank you much for giving detailed explanation.
But
I have a question-
But why we are saying takeWhile is a short circuiting stateful operation.

takeWhile does retain the state of element. It is only breaking the pipeline once match the condition. Then it should be short circuiting stateless operation.

I am not clear this point takeWhile is short circuiting stateful operation.

Why??

smratikushwah
Автор

Hi abhilash just one correction
Stream object doesn't return new stream after applying operations on elements.
Stream only defines logic to process it and define pipeline.
It only returne new stream object once. Not for every operation.

To give and exmple
If after applying suppose filter new stream object is returned after applying logic on every element it would create many new stream... Which doesn't happen
As elements are passes in pipeline like one element gets passed in filter then map
Then again 2nd element and so on..
Please correct me if i am wrong.

rohitgavande
Автор

for output guess question ans is b) 54321

trendingthoughts