I am trying to replace below code block with optional, just want to check will there be any bad implications of doing this like performance or anything as such?
Existing code:
UserObj userObj=new UserObj();
Object result = fetchDetails();
if (null != result) {
userObj.setResult(result.toString());
}
With Java 8 Optional
:
UserObj userObj=new UserObj();
Optional.ofNullable(fetchDetails()).ifPresent(var -> userObj.setResult(var.toString()));
Doing this change just to make code look concise as there are lot of null check blocks in my code.