- Optional.empty() - An empty optional
- Optional.of(t) - returns a present Optional containing t. (t must be non-null)
- Optional.ofNullable(t) - returns an Optional with t that can be null
- Never return null from a method that's supposed to return an optional. It defeats the purpose of Optional
- Never do
opt.get()
unless you can prove that the optional is present. - Prefer alternatives to using
opt.isPresent()
followed byopt.get()
- It's generally a bad idea to create an Optional for the sole purpose of chaining methods from it to get a value.
- If an Optional chain is nested or has an intermediate result of Optional<Optional>, it is probably too complex.
- orElse(default) - Returns value if present or else a default value
Optional<Data> opt = .... Data data = opt.orElse(DEFAULT_VALUE)
- orElse(supplier) - Returns value if present or else gets a default value by calling a supplier
Optional<Data> opt = .... Data data = opt.orElseGet(Data::new)
- orElseThrows(exSupplier) - Returns value if present or else throws an exception obtainer from a supplier
Optional<Data> opt = .... Data data = opt.orElseThrows(IllegalStateException::new)
Optional<Customer> opt = ....
Instead of doing, return opt.isPresent() ? opt.get().getName() : "UNKNOWN"
Use map to do, return opt.map(Customer::getName().orElse("UNKNOWN");
If the value is present,
map()
transforms or maps the value into another value and returns the result in an optional.If value is not present, it returns an empty Optional.
The mapper function is called on the value only when it is present.
Examples:
Optional.of(10).map(x -> x + 10)
===> Optional.of(20)
Optional.empty().map(x -> x + 10)
===> Optional.empty()
Like map()
but transforms the value inside an optional using a functions that returns an optional (x -> Optional.of(x)
).
Examples:
Optional.of(10).flatMap(x -> Optional.of(x + 10))
===> Optional.of(20)
Optional.empty().flatMap(x -> Optional.of(x + 10))
===> Optional.empty()
Optional<Configuration> oParent = config.parent()
Code:
if (!oParent.isPresent() || oParent.get() != this.config()) {
throw new IllegalArgumentException()
}
That can be replaced with filter as follows:
config.parent.filter(config -> config == this.config()).orElseThrow(IllegalArgumentException::new)
Optional<Task> oTask = getTask(....)
Instead of doing this:
if(oTask.isPresent()) {
executor.runTask(oTask.get());
}
Do:
getTask(...).ifPresent(task -> executor.runTask(task))
Or better:
getTask(...).ifPresent(executor::runTask)