How to add string text to orElse and .map

Viewed 68

In the below code, I would like to add a log statement to both of .map and .orElse to indicate whether or not the value is true/false. In other words, in .map i would like to add "........true"

inside orElse i would to add "........false"

How can I modify the belwo code to include both of the aforementioned Strings.

code:

return OptionalsUtils.toOptional(this.getBuiltMovieRoomPersistentDatabase())
            .map(builderObj -> builderObj.isOpen())
            .orElse(false);
1 Answers

You could possibly do that using Optional.orElseGet which expects a Supplier as:

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;
        });
Related