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

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

在 Java 中使用 Optional 和 lambdas

在 Java 中使用 Optional 和 lambdas

慕尼黑8549860 2022-06-15 15:43:41
我正在使用 Java 中的 Optional 和 lambdas 編寫代碼,并且想知道在以下情況下最好的方法是什么:public Optional<BObject> readIndexMaybe(String ref) {    try {        return Optional.ofNullable(index.read(ref)).map(BObjectBuilder::build);    } catch (IOException e) {        LOGGER.error(String.format("Could not read index of ref: %s, error: %s", ref, e));    }    return Optional.empty();}public Optional<BObject> readMaybe(String ref) {    Optional<BObject> bObject = readIndexMaybe(ref);    return bObject.flatMap(boMaybe -> {                <---- HERE        try {            LOGGER.debug(String.format("Object read: %s", ref));            BObject obj = new BObjectBuilder(boMaybe).stream(loadDocumentStream(boMaybe)).build();            return Optional.of(obj);        } catch (IOException e) {            LOGGER.error(String.format("Could not read file with ref: %s, error: %s", ref, e));        }        return Optional.empty();    });}Optional<BObject>使用返回 an然后使用它作為返回類型flatMap接收的 lambda 函數(shù)會(huì)更好,還是在 lambda 內(nèi)部返回然后只使用會(huì)更好:Optional<BObject>nullmappublic Optional<BObject> readIndexMaybe(String ref) {   try {       return Optional.ofNullable(index.read(ref)).map(BObjectBuilder::build);   } catch (IOException e) {       LOGGER.error(String.format("Could not read index of ref: %s, error: %s", ref, e));   }   return Optional.empty();}public Optional<BObject> readMaybe(String ref) {   Optional<BObject> bObject = readIndexMaybe(ref);   return bObject.map(boMaybe -> {              <--- HERE       try {           LOGGER.debug(String.format("Object read: %s", ref));           return new BObjectBuilder(boMaybe).stream(loadDocumentStream(boMaybe)).build();       } catch (IOException e) {           LOGGER.error(String.format("Could not read file with ref: %s, error: %s", ref, e));       }       return null;   });}第一種方法對(duì)我來(lái)說似乎稍微好一點(diǎn),因?yàn)槲铱赡軙?huì)在其他地方重用 lambda 函數(shù),但它不會(huì)返回null但是Optional. 但是,只要我只在一個(gè)地方使用它就值得嗎?
查看完整描述

2 回答

?
UYOU

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

如果您有選擇,我建議編寫返回Optional而不是 null 的函數(shù),如果無(wú)結(jié)果是正常且預(yù)期的結(jié)果。因此,您將使用flatMap調(diào)用此類函數(shù)。該map操作對(duì)于始終返回有效結(jié)果的函數(shù)很有用。它還將 null 返回轉(zhuǎn)換為 empty Optional,這主要在您需要適應(yīng)無(wú)法更改的返回 null 的代碼時(shí)有用。


通常的建議是提取方法而不是使用多行語(yǔ)句 lambda。然后,在操作中使用簡(jiǎn)單的 lambda 或方法引用flatMap。


此外,對(duì)于 try/catch 塊,我建議將 try 子句中的代碼最小化為僅可以實(shí)際拋出您正在捕獲的異常的代碼,并在 try 語(yǔ)句之外進(jìn)行其他轉(zhuǎn)換。在這種情況下,我假設(shè)index.read(ref)andloadDocumentStream(boMaybe)是可以拋出的語(yǔ)句IOException。請(qǐng)注意,這意味著局部變量需要保存臨時(shí)結(jié)果,并且它可以為空。我認(rèn)為這沒關(guān)系。null 處理非常本地化,它允許您將返回的創(chuàng)建合并Optional到單個(gè)表達(dá)式中。


最后,我建議不要對(duì)可選項(xiàng)使用后綴“也許”。這令人困惑,并且在示例boMaybe中flatMap操作的 lambda 參數(shù)不正確。僅當(dāng)值存在時(shí)才評(píng)估該 lambda,因此沒有“可能”。


應(yīng)用所有這些建議給出了結(jié)果代碼:


Optional<BObject> readIndex(String ref) {

    Index i = null;

    try {

        i = index.read(ref);

    } catch (IOException e) {

        LOGGER.error(/*...*/);

    }

    return Optional.ofNullable(i).map(BObjectBuilder::build);

}


Optional<BObject> load(BObject bo) {

    DocStream docStream = null;

    try {

        LOGGER.debug(/*...*/);

        docStream = loadDocumentStream(bo);

    } catch (IOException e) {

        LOGGER.error(/*...*/);

    }

    return Optional.ofNullable(docStream)

                   .map(ds -> new BObjectBuilder(bo).stream(ds).build());

}


Optional<BObject> read(String ref) {

    return readIndex(ref).flatMap(bo -> load(bo)); // or this::load

}


查看完整回答
反對(duì) 回復(fù) 2022-06-15
?
小唯快跑啊

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

您可能希望提供一個(gè)穩(wěn)定且安全的 api,這就是您使用 的原因Optional,盡管在您的情況下它會(huì)使事情變得有點(diǎn)復(fù)雜,因?yàn)槟枰东@已檢查的異常。我建議這種方法:


public Optional<BObject> readIndexMaybe(String ref) {

   try {

       return Optional.ofNullable(index.read(ref)).map(BObjectBuilder::build);

   } catch (IOException e) {

       LOGGER.error(String.format("Could not read index of ref: %s, error: %s", ref, e));

   }

   return Optional.empty();

}


public Optional<BObject> readMaybe(String ref) {

   Optional<BObject> bObject = readIndexMaybe(ref);

   if(!bObject.isPresent()){

      return bObject; // is same as Optional.empty()

   }

   BObject boMaybe = bObject.get();

   try {

      LOGGER.debug(String.format("Object read: %s", ref));

      boMaybe = new BObjectBuilder(boMaybe).stream(loadDocumentStream(boMaybe)).build();

      return Optional.of(boMaybe);

   } catch (IOException e) {

      LOGGER.error(String.format("Could not read file with ref: %s, error: %s", ref, e));

      return Optional.empty();

   }

}

這與您的地圖方法非常相似,但我認(rèn)為它更清楚,因?yàn)槟鷽]有try-catch內(nèi)部 lambda。


查看完整回答
反對(duì) 回復(fù) 2022-06-15
  • 2 回答
  • 0 關(guān)注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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