Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法引用中的返回类型错误:无法将员工转换为可选<;U>;_Java_Lambda_Optional_Flatmap - Fatal编程技术网

Java 方法引用中的返回类型错误:无法将员工转换为可选<;U>;

Java 方法引用中的返回类型错误:无法将员工转换为可选<;U>;,java,lambda,optional,flatmap,Java,Lambda,Optional,Flatmap,我正试图编写一个lambda函数来获取员工位置偏好,并具有下面的代码示例 但是对于lambda函数,我在flatMap(this::buildEmployeeGeolocation) 在方法引用中说返回类型错误:无法将com.abc.employeegeocation转换为java.util.Optional 我错过了什么 public Optional<EmployeeGeolocation> getEmployee(final SessionId sessionId) {

我正试图编写一个lambda函数来获取员工位置偏好,并具有下面的代码示例

但是对于lambda函数,我在
flatMap(this::buildEmployeeGeolocation)
在方法引用中说
返回类型错误:无法将com.abc.employeegeocation转换为java.util.Optional

我错过了什么

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();
}
public可选getEmployee(最终SessionId SessionId){
返回可选的.ofNullable(EmployeeReferenceStore.getEmployeeAccountPreferences(sessionId))
.map(首选项->首选项.getPreference(首选项键.Location))
.filter(StringUtils::isNotBlank)
.map(此::readEmployeelocation)
.flatMap(此::buildEmployeeGeolocation);
}
私有可选readEmployeeLocation(@NonNull最终字符串encryptedGeolocation){
试一试{
返回可选的.ofNullable(objectMapper.readValue(encryptedGeolocation,EmployeeGeolocation.class));
}捕获(最终IOE例外){
log.error(“读取加密地理位置时出错”);
抛出新的运行时异常(e);
}
}
私有EmployeeGeolocation buildEmployeeGeolocation(@NonNull final EncryptedGeolocation UnderedEncryptedGeoLocation){
return EmployeeGeolocation.builder()
.纬度(10.0)
.经度(10.0)
.准确度(1.0)
.locationType(地址)
.build();
}

看起来您真正需要做的是交换
地图
平面地图
。更改代码

.map(this::readEmployeeLocation) 
.flatMap(this::buildEmployeeGeolocation);

.flatMap(this::readEmployeeLocation)//因为您已经有了一个可选的
.map(this::buildEmployeeGeolocation);//以上结果是可选的
重要:根据代码
可选.ofNullable(…).map(…).filter(StringUtils::isNotBlank)
推断,在执行此操作之前,它将导致
可选

.flatMap(this::readEmployeeLocation) // since you already have an Optional<String>
.map(this::buildEmployeeGeolocation); // above results in Optional<EncryptedGeolocation>