| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Java 9, 10, 11 Optional API news overview.
Reference: http://iteratrlearning.com/java9/2016/09/05/java9-optional.html
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) - it is a way of simplifying such task:
Optional<Person> person = get(...);
if (person.isPresent()) {
render(person.get());
} else {
renderEmpty();
}
just to:
get(...).ifPresentOrElse(X::render, X::renderEmpty)
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier) - it is a way of simplifying:
assumptions:
Optional<Person> findByName(...) Optional<Person> findById(...)
then something like this:
Optional<Person> person =
Optional.ofNullable(
findByName(person.getName())
.orElseGet(() -> findById(person.getId())
));
could be rewritten just to:
Optional<Person> person =
findByName(person.getName())
.or(() -> findById(person.getId());
public Stream<T> stream() - please refer my other github project: java-converting-optionals-list
public T orElseThrow() - exact copy of get() but with more meaningful name:
public T get() {
if (value == null) {
throw new NoSuchElementException("No value present");
}
return value;
}
public T orElseThrow() {
if (value == null) {
throw new NoSuchElementException("No value present");
}
return value;
}
Remark: https://twitter.com/briangoetz/status/665239052380012548?lang=en
In hindsight, calling it get was a (my) mistake. Just too easy to call get() w/o thinking.
@BrianGoetz
Remark: get() will be deprecated: http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-April/040531.html
public boolean isPresent() {
return value != null;
}
public boolean isEmpty() {
return value == null;
}
We provide tests for mentioned above methods.
| Back | FazBrowse Home | New Git URL |