java-streams-course's People
Forkers
rcubila amyaim992 emmahyde vinodhthiagarajan1309 cguca prajesh-ananthan pradeepk2112 arvindve mramalingam maghi711 paul41smith timrsfo ambaree camilogaspar rahulnpt emanonk mikita-simanavets andreivrp joserfjuniorllms nataliiakushnirenko srkadiyala edusp kirandz611 oriol-rius annasamonenko sushildw9 subhashsurendran sumitml teju068 przybylodawid sergion epredator reddyd646 vbkonar avishek1987 vicrosa25 marekkristof hari819 alialrabi contactbarun70 bharathkhm pankajrana737 mukeshchandraswami ranavivek04 fearaus elahayder sattiwari alexandreunfried gorkemgenc kkyungd pra4pra vaibhav2324 rexvips jaco11 lucascamp0s rb27123 kpkar doytsujin sffej ankit-tyagi gavinwang-au tsundara parthapratim-das harisharavind khilanbshah erturcot sammysheng sammyzysheng udemyprojects talc001 suvinamathur dennishangithub svkdey mukeshupreti2006 alptekintalan yuriko-pixel merulappan desmondabraham29 satishppawar jy2054050 kn3kaihatsu ethanmiller1 mdorjbat mkunapareddy tronwa dennismaas daveprochazka sbrytskyy mrthiago sumitluthra vedantchimote selvester69 daskoushik sherman-zhou swatibhat123 raireena10 jithin012 marwansubhim rspixers barnalidasjava-streams-course's Issues
orElse() vs orElseGet()
Was going through the section-8 and saw the findFirst()
and findAny()
. I saw you have explained the orElse() and orElseGet() in a very mild way.
I would recommend to add the proper explanation in future update of your course as many people have a great deal of confusion between them when they deal with Optional.
My explanation would be:
The difference is pretty subtle and if you dont pay much attention then you will keep it using in a wrong way.
Best way to understand the difference between orElse() and orElseGet() is that orElse() will always be executed if the Optional is null or not, But orElseGet() will only be executed when Optional is null.
The dictionary meaning of orElse is :- execute the part when something is not present, but here it contradicts, see the below example:
Optional<String> nonEmptyOptional = Optional.of("Vishwa Ratna");
String value = nonEmptyOptional.orElse(iAmStillExecuted());
public static String iAmStillExecuted(){
System.out.println("nonEmptyOptional is not NULL,still I am being executed");
return "I got executed";
}
Output: nonEmptyOptional is not NULL,still I am being executed
Optional<String> emptyOptional = Optional.ofNullable(null);
String value = emptyOptional.orElse(iAmStillExecuted());
public static String iAmStillExecuted(){
System.out.println("emptyOptional is NULL, I am being executed, it is normal as
per dictionary");
return "I got executed";
}
Output: emptyOptional is NULL, I am being executed, it is normal as per dictionary
For orElseGet()
, The method goes as per dictionary meaning, The orElseGet()
part will be executed only when the Optional is null.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.