0% found this document useful (0 votes)
3 views

Java Questions

The document explains the Optional class in Java 8+, which is a container that may or may not hold a non-null value, helping to avoid NullPointerExceptions. It details how to create Optional instances using factory methods (empty, of, and ofNullable) and describes various instance methods like get, isPresent, ifPresent, and orElse for handling values. Additionally, it covers methods for transforming results, such as filter, map, and flatMap, emphasizing best practices for using Optionals in Java programming.

Uploaded by

udhayapps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Questions

The document explains the Optional class in Java 8+, which is a container that may or may not hold a non-null value, helping to avoid NullPointerExceptions. It details how to create Optional instances using factory methods (empty, of, and ofNullable) and describes various instance methods like get, isPresent, ifPresent, and orElse for handling values. Additionally, it covers methods for transforming results, such as filter, map, and flatMap, emphasizing best practices for using Optionals in Java programming.

Uploaded by

udhayapps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

OPTIONALS

Java 8+

What are Optionals?

Optional<T> is a generic class - belongs to java.util


package. It is a value-based class.

Instance of an Optional class is a container object which


may or may not contain a non-null value.

Empty Optional represents absence of value.

Optional class offer a way to handle situation in which


value may or may not be present.

Prior to Optionals, you would use value null to indicate that


no value is present. However, this can lead to
NullPointerException if an attempt is made to dereference
a null reference.

To avoid frequent null checks, Optional classes offer better


way of handling such situations.

#CodingWalaShree
OPTIONALS
Java 8+
1. Creating Optionals

Optional class has a private constructor.


It provides three factory methods (empty, of and
ofNullable) to create Optional class instance.

1.1 empty( ):

Empty Optional represents


absence of value.

empty() is static factory method that creates empty


Optional instance of given type.

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+

1.2 of( ):

of() is static factory method that creates an Optional


instance with the specified present non-null value.

Throws NullPointerException if value is null

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( ):

ofNullable() is static factory method that returns an


Optional describing the specified value, if non-null,
otherwise returns an empty Optional.

Should be used over of() method if value may or may not be


null.

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+
2. Instance Methods - Unwrap Optionals

Most of the instance methods depend on the presence


or absence of a contained value are provided.

2.1 get( ):

If a value is present in this Optional, returns the value,


otherwise throws NoSuchElementException.

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+

2.2 isPresent( ):

Returns true if there is a value present


Returns false if a value is absent.

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+

2.3 ifPresent( ):

If a value is present, invoke the specified consumer with


the value, otherwise do nothing.

Consumer<T> -- Has method void accept(T t) that takes an


argument and returns nothing i.e. consumes the argument.

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+

2.4 orElse( ):

Return the value if present,


Return other if the value is absent.

In other words, return the value if present,


Return a default value if a value is absent

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+

2.5 orElseGet( ):

Returns the value if present, otherwise invoke the supplier


to get result of that invocation.

Supplier<T> -- has method T get() that supplies (returns) a


value.

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+

2.6 orElseThrow( ):

Return the contained value, if present, otherwise throw an


exception to be created by the provided supplier.

Supplier<X> -- has method X get() that supplies (returns) an


exception to be thrown if value is absent.

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+
3. Instance Methods - Restrict / transform result

Most of the instance methods depend on the presence


or absence of a contained value are provided.

Consider below model classes:

#CodingWalaShree
OPTIONALS
Java 8+
3.1 filter( ):

If a value is present, and the value matches the given


predicate, return an Optional describing the value,
otherwise return an empty Optional.

Restricts the result by matching the given predicate.

Predicate<T> -- Has method boolean test(T t) that matches


given condition for value wrapped by Optional.

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+

3.2 map( ):

If a value is present, apply the provided mapping function


to it, and if the result is non-null, return an Optional
describing the result. Otherwise return an empty Optional.

Transforms the result with given mapper function.

Function<T, R> -- Has method R apply(T t) that applies


function to given argument T and maps to result of type R.

Similar to filter method, returns Optional of some type and


hence allows method chaining.

#CodingWalaShree
OPTIONALS
Java 8+

Example:

Output

#CodingWalaShree
OPTIONALS
Java 8+
3.3 flatMap( ):

If a value is present, apply the provided Optional-bearing


mapping function to it, return that result, otherwise return
an empty Optional.

Flattens and Transforms the result with given mapper


function.

#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

You might also like