C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Optional.of: This creates a new Optional based on the argument. It essentially wraps the argument in an Optional class.
Optional.empty: This create an Optional that wraps no value. The isPresent method will return false.
Get: We use get() to return an Optional's value. But be careful—this will cause an exception if no value is present. Call isPresent first.
Java program that uses Optional Integer
import java.util.Optional;
public class Program {
public static void display(Optional<Integer> value) {
// Handle the Optional argument.
if (value.isPresent()) {
System.out.println(value.get());
} else {
System.out.println("NOT PRESENT");
}
}
public static void main(String[] args) {
// Call display with optional arguments.
display(Optional.of(10));
display(Optional.empty());
}
}
Output
10
NOT PRESENT
And: If no value is present, it will return the argument (much like getOrDefault on a HashMap).
HashMapJava program that uses Optional orElse
import java.util.Optional;
public class Program {
public static void main(String[] args) {
// Create an empty Optional.
Optional<Integer> option = Optional.empty();
// Use orElse to get value or the argument if no value is present.
int result = option.orElse(100);
System.out.println(result);
}
}
Output
100
Here: We introduce an identifier x which refers to the inner value. We call println with x as the argument.
Void: The method called on the right side of the lambda must be a Consumer, and return no value (it must be void).
PrintlnJava program that uses ifPresent
import java.util.Optional;
public class Program {
public static void main(String[] args) {
// Print an optional if its value is present.
Optional<Integer> option = Optional.of(10);
option.ifPresent(x -> System.out.println(x));
// When the value is not present, nothing is printed.
option = Optional.empty();
option.ifPresent(x -> System.out.println(x));
}
}
Output
10
Caution: The of method will causes a NullPointerException if we call it with a null argument.
ExceptionsJava program that uses ofNullable, of
import java.util.Optional;
public class Program {
public static void main(String[] args) {
String test = "cat";
// If a value may be null, use ofNullable.
Optional<String> option = Optional.ofNullable(test);
System.out.println(option.get());
// The of method will throw an exception on a null value.
String test2 = null;
Optional<String> option2 = Optional.of(test2);
System.out.println(true); // Not reached.
}
}
Output
cat
Exception in thread "main" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Unknown Source)
at java.util.Optional.<init>(Unknown Source)
at java.util.Optional.of(Unknown Source)
at program.Program.main(Program.java:15)
Note: Some functions are expressed in a clearer way with Optional arguments. This can reduce code duplication.
Note 2: With Optional, we can avoid rewriting a method for a slightly different set of parameters.
And: With this class, we eliminate confusing "special values" in types like int that mean "no value."
Tip: For performance-critical methods, using separate method versions instead of Optional values is worth consideration.