mardi 5 janvier 2021

How to use Optional's map and orElseGet methods when calling a void method based on the optional value

I have a Java Optional variable that I'm using as below.

Optional<Forms> formsOptional = input.data().get().forms();
if(formsOptional.isPresent()) {
    this.doSomething(myValue, forms.get());
} else {
    this.doSomething(myValue, Forms.builder().names(myList).build());
}

In this case, doSomething method is a void method that does something. As this is a void method, I'm confused about how I can use the map().orElseGet(), as I don't have anything to transform as well. Any suggestions to optimize this piece of code using any Java8 Optional techniques would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire