mercredi 18 mars 2015

Inline IF statement with Optional object in Java

I want to write an inline IF where the returned object is Optional, something like this:



import com.google.common.base.Optional;
public Optional<String> getFirstElement(String str, String separator) {
final String[] strs = str.split(separator);
return strs.length == 0 ? Optional.absent() : Optional.of(strs[0]);
}


However, Optional.absent() has type Optional<?> instead of Optional<String>. Any idea about how to solve this?


Aucun commentaire:

Enregistrer un commentaire