So I have been using Guava's Optional
for a while now, and I'm moving to Java 8 so I wanted to use its Optional
class, but it doesn't have my favorite method from Guava: asSet()
. Is there a way to do this with the Java 8 Optional
that I am not seeing. I love being able to treat Optional
as a collection so I can do this:
for (final User u : getUserOptional().asSet()) {
return u.isPermitted(getPermissionRequired());
}
In some cases avoids the need for an additional variable.
For example:
Optional<User> optUser = getUserOptional();
if (optUser.isPresent()) {
return optUser.get().isPermitted(getPermissionRequired());
}
Is there an easy way to replicate the Guava style in Java 8's Optional
?
Thanks