Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Optional asSet()

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

like image 352
csyperski Avatar asked Aug 09 '14 13:08

csyperski


People also ask

What is an Optional class in Java 8?

Advertisements. Optional is a container object used to contain not-null objects. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as 'available' or 'not available' instead of checking null values.

How do I retrieve an object from Optional?

Retrieving the Value From Java 8 Optional Object Using get() Method. The get() method of Optional is simply used to return a value from the Optional object. Suppose the value is not present, then it throws the exception NoSuchElementException.

What is the use of Optional empty () in Java?

Optional class in Java is used to get an empty instance of this Optional class. This instance do not contain any value. Parameters: This method accepts nothing. Return value: This method returns an empty instance of this Optional class.


3 Answers

There is a simple way of converting an Optional into a Set. It works just like any other conversion of an Optional:

Given an Optional<T> o you can invoke

o.map(Collections::singleton).orElse(Collections.emptySet())

to get a Set<T>. If you don’t like the idea of Collections.emptySet() being called in every case you can turn it into a lazy evaluation:

o.map(Collections::singleton).orElseGet(Collections::emptySet)

however, the method is too trivial to make a performance difference. So it’s just a matter of coding style.

You can also use it to iterate like intended:

for(T t: o.map(Collections::singleton).orElse(Collections.emptySet()))
    // do something with t, may include a return statement
like image 71
Holger Avatar answered Oct 19 '22 20:10

Holger


You appear to only be using asSet so you can write a for loop, but that's unnecessary in Java 8. Instead of your code

Optional<User> optUser = getUserOptional();
if ( optUser.isPresent() ) {
    return optUser.get().isPermitted(getPermissionRequired());
}

you could write

getUserPresent().map(optUser -> optUser.isPermitted(getPermissionRequired()))
   .orElse(false);

...or, in many cases, you could use Optional.ifPresent(Consumer<T>).

like image 27
Louis Wasserman Avatar answered Oct 19 '22 21:10

Louis Wasserman


You can use map :

return optUser.map(u -> u.isPermitted(getPermissionRequired()));

But it would return an Optional<WhateverTypeIsPermittedReturns>.

Reference

public Optional map(Function mapper)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.

like image 2
Eran Avatar answered Oct 19 '22 20:10

Eran