How to add string literals to orElse and .map… here is a solution to the problem.
How to add string literals to orElse and .map
In the code below, I want to add a log statement to .map and .orElse to indicate if the value is true/false. In other words, in the .map I want to add
“………… Really”
In orElse I would add
“………… fake”
How to modify the belwo code to include the above two strings.
Code:
return OptionalsUtils.toOptional(this.getBuiltMovieRoomPersistentDatabase())
.map(builderObj -> builderObj.isOpen())
.orElse(false);
Solution
You can use Optional.orElseGet
to do this, it expects the Supplier
to:
return OptionalsUtils.toOptional(this.getBuiltMovieRoomPersistentDatabase())
.map(builderObj -> {
System.out.println("Its true here.");
return builderObj.isOpen();
})
.orElseGet(() -> {
System.out.println("Its false here");
return false;
});