In short - no. There is nothing wrong or complicated with this code that is solvable with lambdas per se. What you should rather do is review what this code actually does and simplify it from there.
My take on it, is if you can not change signature of isValuePresent, then do something like this:
boolean isValuePresent(String id, Optional<String> option) {
if (!option.isPresent()) { // this already saves us loading the config if we've got no value to check against
return false;
}
FeatureClass features = configStorage.getConfig().getConfigMap().get(id);
if (features == null) { // replaces containsKey().get() with one call. It is hardly an improvement, just my stylistic choice
return false;
}
String value = option.get();
if (features.getset().contains(value) { // we actually have no need to check if set is empty or not, we rather have it handle that itself
log.info(String.format("value present for id %s", id));
return true;
}
return false;
}
Or, if you can change signatures, I think it's better to have something like this:
boolean isValuePresent(String id, String value) {
FeatureClass features = configStorage.getConfig().getConfigMap().get(id);
if (features == null) {
return false;
}
if (features.getset().contains(value)) {
log.info(<snip>);
return true;
}
return false;
}
And call it like this:
String id = loadId();
Optional<String> myValue = loadMyValue(id);
return myValue.map(value -> Checker.isValuePresent(id, value)).orElse(false);
As you can see, there is no lambdas, because there's no need for them - at no point here we are required to change our behavior, there's no strategy in this method that could possibly be plugged in from outside.
Now, about why I chose to not have Optional in argument:
- It offers more flexibility to callers of your method. If they have value
String form already, they will not be required to wrap it in useless Optional before calling your method.
- Using
Optional as method argument type looks ugly, because that class is designed for usage as return value, and doesn't offer much as argument type besides isPresent() and get(). Compare this to good number of controlled value extraction methods when it is used as return value (to name a few, orElse, orElseGet, map, flatMap, and even more to come in Java 9).
java.util.Optional is not Serializable, which puts an end to call your method via remote interface (this, of course, implies that you care about calling it via remote interface).
Optional<String>as method input? Surely you can check if it's present beforehand and just assume that "value is not present"?