Java 8 Short Circuit operation Stream with Demo| Coding Interview Questions and Answers |Code Decode

preview_player
Показать описание
In this video of Code Decode we have covered Java 8 Short Circuit operation which is important in terms of Java 8 Interview questions for freshers and experienced candidates

Udemy Course of Code Decode on Microservice k8s AWS CICD link:

Course Description Video :

Java 8 short-circuiting operations are just like boolean short-circuit evaluations in Java.

In boolean short-circuiting logic, for example firstBoolean && secondBoolean, if firstBoolean is false then the remaining part of the expression is ignored (the operation is short-circuited) because the remaining evaluation will be redundant. Similarly in firstBoolean || secondBoolean, if firstBoolean is true the remaining part is short-circuited.

Java 8 Stream short-circuit operations are not limited to boolean types. There are pre defined short-circuiting operations.

Java 8 stream intermediate and terminal operations both can be short circuiting.
Intermediate - limit()
Terminal - findFirst(), findAny(), anyMatch(), allMatch(), noneMatch()

This method takes one (long N) as an argument and returns a stream of size no more than N

Stream limit(long N) - Where N is the number of elements the stream should be limited to and this function returns new stream as output.

As soon as limit() reaches the maximum number of items, it doesn't consume any more items and simply returns the resulting stream. Hence, we say that limit() is a short-circuiting operation.

limit() returns the first n elements in the encounter order and not just any n elements.
This is an intermediate operation.

This is a good choice when working with infinite streams or infinite values.

limit() method is invoked after calling the terminal operation such as count() or collect() methods.

This returns a stream of elements with the size or max limit given.

This works well in sequential streams. Not suggested using in the parallel streams such as larger or higher in size.

maxSize can not be negative. If negative then it will throw IllegalArgumentException.

Optional findFirst()

Returns the very first element (wrapped in Optional object) of this stream and before traversing the other.

The terminal operation terminated on finding the first element, hence short-circuited.

findAny()

Returns an Optional instance which wraps any and only one element of the stream.

The behavior of this operation is explicitly nondeterministic; it is free to select any element in the stream. This is to allow for maximal performance in parallel operations; the cost is that multiple invocations on the same source may not return the same result. (If a stable result is desired, use findFirst() instead.

For a sequential stream there won't be any difference between 'findFirst' and 'findAny'. But for a parallel stream findAny will return 'any' element rather than waiting for the 'first' element.

boolean anyMatch()

Tests whether any elements of this stream match the provided predicate. This terminal method will return as soon as it finds the match and will not transverse all the remaining elements to apply the predicate.

boolean allMatch()

Tests whether all elements match the provided predicate. It may return early with false result when any element doesn't match first.

boolean noneMatch()

Tests whether no elements of this stream match the provided predicate. It may return early with false result when any element matches the provided predicate first.

Subscriber and Follow Code Decode

#codedecode #java8shortcicrcuit #javainterviewquestionsandanswers
Рекомендации по теме
Комментарии
Автор

Awesome teaching skill you have, I think by doing just go throught your videos i can clear any java interview 😊🔥

ashwinalapure
Автор

your videos are helping the testing community... your contribution is immense... kudos to your team..👏👏

krishnakarnakoti
Автор

Explained really well. Please cover the rest of optional topic too. you are a greatttt mentor :)

gayathris
Автор

You are great ma'am. Lot of questions are cleared. Requesting for optional.

vishalsvisual
Автор

Java8 became easy coz of you ..Thankyou :)

lakshmipg
Автор

We thoroughly enjoy learning and stay connected with you all the time till the end of the all videos, this is big achievements for you. Thanks a lot.
GOD BLESS YOU !!

shaileshgupta
Автор

Thank You Mam. Excellent explanation. You Nailed it. Please cover optional also.

vengateshm
Автор

Great explanation without practicing i got a clear picture on the topics

arjun
Автор

Thanks for this explanation... yes please make a continuation on Optional....

dpkpk
Автор

Stream well frame work explanation required mam.

malaiarasi
Автор

Big fan of your videos. Please cover Optional API, Executor framework

asthagupta
Автор

Java 8 optional interview questions please add. Thanks.
Also few spring boot microservices and fault tolerance more videos on this.
Also if possible can you do apart from java like on GCP cloud or python.
Your videos and contect are so useful and understands easily. 👍 Thanks for making such amazing videos.

namratadange
Автор

please cover stream builder and stream support.. thank you so much your videos really helpful

meghaalugade
Автор

Thank you Code Decode for videos, very nice explanation with examples. PLEASE post the REACTJS interview preparation as well if possible. Thanks

vivekvengala
Автор

@CodeDecode Thanks for all the detailed videos...Please cover topics like Time and space complexity, Big O notation etc

SuperMohit
Автор

I want to appreciate you like Amir Khan in dangal 🤪 " Shbahas beta " 🤭 but I am not that much old 😂
Keeping rocking 💫 yaar😉💚

hariprasad
Автор

Thank you so much for your hard work and easy explanation. Can i get the above source code.

MrAsgar
Автор

Can you please do videos for methods under Collectors

arthiarulkumar