亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

方法引用中的返回類型錯誤:無法將 Employee 轉換為Optional<U>

方法引用中的返回類型錯誤:無法將 Employee 轉換為Optional<U>

qq_遁去的一_1 2023-12-10 15:12:25
我正在嘗試編寫一個 lambda 函數來獲取員工位置偏好并具有下面的代碼示例。flatMap(this::buildEmployeeGeolocation) 但對于我的 lambda 函數,我在說 時遇到編譯錯誤Bad return type in method reference: cannot convert com.abc.EmployeeGeolocation to java.util.Optional<U>。我在這里缺少什么?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();}
查看完整描述

1 回答

?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

看來您真正需要做的是交換mapflatMap。更改代碼

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

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

重要提示:從代碼中推斷Optional.ofNullable(...).map(...).filter(StringUtils::isNotBlank),它將導致Optional<String>直到此操作。


查看完整回答
反對 回復 2023-12-10
  • 1 回答
  • 0 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號