Java Questions
Java Questions
Java 8+
#CodingWalaShree
OPTIONALS
Java 8+
1. Creating Optionals
1.1 empty( ):
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
1.2 of( ):
Should be used only when you know that the value is non-
null; otherwise, use ofNullable() method described next.
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
1.3 ofNullable( ):
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
2. Instance Methods - Unwrap Optionals
2.1 get( ):
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
2.2 isPresent( ):
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
2.3 ifPresent( ):
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
2.4 orElse( ):
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
2.5 orElseGet( ):
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
2.6 orElseThrow( ):
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
3. Instance Methods - Restrict / transform result
#CodingWalaShree
OPTIONALS
Java 8+
3.1 filter( ):
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
3.2 map( ):
#CodingWalaShree
OPTIONALS
Java 8+
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
3.3 flatMap( ):
#CodingWalaShree
OPTIONALS
Java 8+
Example:
Output
#CodingWalaShree
OPTIONALS
Java 8+
Subscribe
CodingWalaShree
Examples in this presentation are covered in my YouTube Video -
🚀 Java 8 Optional Class | Say Goodbye to NullPointerExceptions |
“
Best Practices & Key Methods”
Note: Explore more detailed and insightful notes to boost your technical
skills and ace technical interviews. Don't forget to check out my other
posts for more valuable content!
Follow me on https://www.linkedin.com/in/shrikrishna-
prabhumirashi-717b2356/ for interesting insights.
#CodingWalaShree