2

I am facing a problem while converting old-school if usage to Optional.ifPresent. Here is the previous version of the code.

State state = State.OK;
final Optional<Person> checkExistingPerson = checkIt();

if(checkExistingPerson.isPresent()) {
    Person person = checkExistingPerson.get();

    if("blah".equals(person.getName())) {
        state = State.DUPLICATE;
    } else {
        state = State.RESTORED;
    }

    return Member(person.getId(), state);
}

return Member(null, state);

And here is the Optional.ifPresent usage

State state = State.OK;
final Optional<Person> checkExistingPerson = checkIt();

checkExistingPerson.ifPresent(person -> {

    if("blah".equals(person.getName())) {
        state = State.DUPLICATE;
    } else {
        state = State.NEW;
    }

    return Member(person.getId(), state);
});

return Member(null, state);

And also, here is the screenshot what IntelliJ forces me to change it. enter image description here

What is the best approach to use Optional regarding to my problem? Thx all!

vtokmak
  • 1,496
  • 6
  • 35
  • 66

4 Answers4

3
  1. External variables inside lambdas need to be final.
  2. ifPresent accepts a Consumer so it has no return value, therefore you can't use return Member(person.getId(), state); (also remember that you are inside a lambda, so return is not going to exit from your method, but from the lambda).

map instead accepts a Function which has a return value, so you could use it instead of ifPresent.

This is how you could refactor your code, by moving state inside the lambda and by using map and orElseGet:

return checkExistingPerson.map(person -> {
    State state;

    if("blah".equals(person.getName())) {
        state = State.DUPLICATE;
    } else {
        state = State.NEW;
    }

    return Member(person.getId(), state);
})
.orElseGet(() -> Member(null, State.OK));
Loris Securo
  • 7,538
  • 2
  • 17
  • 28
3

You can't change the value of a local variable from within a lambda expression. Besides, ifPresent can't return a value, as its return type is void.

Instead, you should use Optional.map, which transform the object wrapped by Optional if it's present:

return checkExistingPerson
    .map(person -> new Member(
            person.getId(), 
            "blah".equals(person.getName()) ? State.DUPLICATE : State.RESTORED))
    .orElse(new Member(null, State.OK));
fps
  • 33,623
  • 8
  • 55
  • 110
1

If you are accessing local variables inside lambda expression then variables should be final or effectively final, which means they can't be modify here, you can also declare variables inside lambda expressions., and topic regarding question ifPresent will take Consumer as argument which has no return type, you use map which will take Function and parameter according to @Loris

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

In the inner method you can't refer to external variables if they are not defined as final, you can change your method like that:

final Optional<Person> checkExistingPerson = checkIt();

checkExistingPerson.ifPresent(person -> {
    State state;
    if("blah".equals(person.getName())) {
        state = State.DUPLICATE;
    } else {
        state = State.NEW;
    }

    return Member(person.getId(), state);
});

return Member(null, State.OK); 
TheOni
  • 810
  • 9
  • 26