Optional streams
Two of Java 8’s tentpole features are optionals and streams. Unfortunately, streaming optionals often involves code like:
Set<Optional<MyType>> mySet = ...
mySet.stream()
.filter(Optional::isPresent)
.map(Optional::get)
.map(MyType::functionICareAbout)
...
Requiring two lines to unwrap optionals and discard the empty ones isn’t the end
of the world, but it has gotten nicer in Java 9. The Optional::stream
method
transforms an optional into a stream that has either zero or one elements.
Therefore, we can write code like:
Set<Optional<MyType>> mySet = ...
mySet.stream()
.flatMap(Optional::stream)
.map(MyType::functionICareAbout)
...
Not life changing, but a little bit nicer than before.