I\'m trying to make something like this:
private String getStringIfObjectIsPresent(Optional object){ object.ifPresent(() ->{
Two options here:
Replace ifPresent with map and use Function instead of Consumer
ifPresent
map
Function
Consumer
private String getStringIfObjectIsPresent(Optional object) { return object .map(obj -> { String result = "result"; //some logic with result and return it return result; }) .orElseThrow(MyCustomException::new); }
Use isPresent:
isPresent
private String getStringIfObjectIsPresent(Optional object) { if (object.isPresent()) { String result = "result"; //some logic with result and return it return result; } else { throw new MyCustomException(); } }