第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 lambda 時(shí)處理空指針

使用 lambda 時(shí)處理空指針

慕桂英546537 2022-12-21 14:44:47
nullPointerException除非e.getKey()返回,否則下面的代碼會(huì)得到嗎nullMap<Integer, Optional<SecurityAttributeChange>> spnCreationDetails;Optional.ofNullable(spnCreationDetails.get(e.getKey()).orElse(new SecurityAttributeChange()).getNewAttribute())                                    .orElse(new SecurityAttr())                                    .getUserName());我在這一行中遇到了 NPE,是否可以調(diào)試它?我知道spnCreationDetails.get(e.getKey())返回 null,我null在這里處理時(shí)遺漏了什么嗎?- 編輯這是幾乎完整的方法,它將有助于提供有關(guān)如何重構(gòu)它以使其可讀的輸入。private void updateAuditFields(List<SecurityAttributeChange> securityChanges, Map<Integer, Map<String, Object>> result) {    Map<Integer, Optional<SecurityAttributeChange>> spnModificationDetails = ...    Map<Integer, Optional<SecurityAttributeChange>> spnCreationDetails = ...    result.entrySet().stream().forEach(e -> {        e.getValue()                .put("userIdLastChanged",                        Optional.ofNullable(Optional.ofNullable(spnModificationDetails.get(e.getKey()))                                .orElse(Optional.of(new SecurityAttributeChange()))                                .get()                                .getNewAttribute()).orElse(new SecurityAttr()).getUserName());        e.getValue()                .put("lastChangedDatetime",                        Optional.ofNullable(Optional.ofNullable(spnModificationDetails.get(e.getKey()))                                .orElse(Optional.of(new SecurityAttributeChange()))                                .get()                                .getNewAttribute()).orElse(new SecurityAttr()).getKnowledgeBeginDate());        e.getValue()                .put("userIdCreated", Optional.ofNullable(                        Optional.ofNullable(spnCreationDetails.get(e.getKey())).orElse(Optional.of(new SecurityAttributeChange())).get().getNewAttribute())                        .orElse(new SecurityAttr())                        .getUserName());    });}
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

如果spnCreationDetails.get(e.getKey())可以為空,您可能需要:

Optional.ofNullable(spnCreationDetails.get(e.getKey()))
        .orElse(new SecurityAttributeChange())
        .getNewAttribute()
        .orElse(new SecurityAttr())
        .getUserName();

iespnCreationDetails.get(e.getKey())應(yīng)該用Optional.

編輯:看到您的評(píng)論,spnCreationDetails.get(e.getKey())可以返回 anull或 an Optional<SecurityAttributeChange>,因此最好只將 thenull轉(zhuǎn)換為 new Optional

spnCreationDetails.getOrDefault(e.getKey(),Optional.empty())
                  .orElse(new SecurityAttributeChange())
                  .getNewAttribute()
                  .orElse(new SecurityAttr())
                  .getUserName();


查看完整回答
反對(duì) 回復(fù) 2022-12-21
  • 1 回答
  • 0 關(guān)注
  • 292 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)