0

I am trying to write a lambda function that gets employee location preference and have the code sample below.

But for my lambda function I get a compilation error at

flatMap(this::buildEmployeeGeolocation)
saying
Bad return type in method reference: cannot convert com.abc.EmployeeGeolocation to java.util.Optional<U>
.

What am I missing here?

public Optional<EmployeeGeolocation> getEmployee(final SessionId sessionId) {
    return Optional.ofNullable(employeePreferencesStore.getEmployeeAccountPreferences(sessionId))
            .map(preferences -> preferences.getPreference(PreferenceKey.Location))
            .filter(StringUtils::isNotBlank)
            .map(this::readEmployeelocation)
            .flatMap(this::buildEmployeeGeolocation);
}

private Optional<EncryptedGeolocation> readEmployeeLocation(@NonNull final String encryptedGeolocation) {
    try {
        return Optional.ofNullable(objectMapper.readValue(encryptedGeolocation, EmployeeGeolocation.class));
    } catch (final IOException e) {
        log.error("Error while reading the encrypted geolocation");
        throw new RuntimeException(e);
    }
}

private EmployeeGeolocation buildEmployeeGeolocation(@NonNull final EncryptedGeolocation unditheredEncryptedGeolocation) {
    return EmployeeGeolocation.builder()
            .latitude(10.0)
            .longitude(10.0)
            .accuracy(1.0)
            .locationType(ADDRESS)
            .build();
}
john Asked question May 13, 2021