y07k2
y07k2

Reputation: 2052

Change isPresent() and method invocation into ifPresent()

How can I change:

if (pAlarms[0].getMoIdentifier().isPresent()) {
    Optional<AlarmValue[]> alarmValues = getAlarmsFromMo(pAlarms[0].getMoIdentifier().get());
}

into ifPresent()?

Upvotes: 0

Views: 155

Answers (2)

Link64
Link64

Reputation: 770

Even though you asked for using ifPresent(), I think using flatMap() makes a lot more sense in your example. With it you should be able to do this:

Optional<AlarmValue[]> alarmValues = pAlarms[0].getMoIdentifier().flatMap(this::getAlarmsFromMo)

Upvotes: 4

Kayaman
Kayaman

Reputation: 73558

This should do it.

pAlarms[0].getMoIdentifier().ifPresent(moId -> {
    Optional<AlarmValue[]> alarmValues = getAlarmsFromMo(moId);
});

Upvotes: 2

Related Questions