Side effect method in option <T>?

I am looking for a way to accomplish a task that can do side effects.

I am trying the following:

getOptionalHumanObject()
    .map((Human h1) -> human::getName)
    .do((String name) -> System.out::printLn)
    .do((String name) -> invokeImportentHttpPostRequest(x))
    .map((String name) -> name.length())
    ...

      

Now the only way I think is to use a filter / map is to do my side effect and return true / the same object.

Are you using additional class methods for side effects before the terminal operation, such as ifPresent?

+3


source to share


1 answer


Why not combine all your operations into one lambda called with ifPresent

?



getOptionalHumanObject()
    .map((Human h1) -> human::getName)
    .ifPresent((String name) -> {
        System.out.printLn(name)
        invokeImportentHttpPostRequest(x)
     });

      

+4


source







All Articles