<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### [Optional 對象操作](https://lingcoder.gitee.io/onjava8/#/book/14-Streams?id=optional-%e5%af%b9%e8%b1%a1%e6%93%8d%e4%bd%9c) 當我們的流管道生成了**Optional**對象,下面 3 個方法可使得**Optional**的后續能做更多的操作: * `filter(Predicate)`:對**Optional**中的內容應用**Predicate**并將結果返回。如果**Optional**不滿足**Predicate**,將**Optional**轉化為空**Optional**。如果**Optional**已經為空,則直接返回空**Optional**。 * `map(Function)`:如果**Optional**不為空,應用**Function**于**Optional**中的內容,并返回結果。否則直接返回**Optional.empty**。 * `flatMap(Function)`:同`map()`,但是提供的映射函數將結果包裝在**Optional**對象中,因此`flatMap()`不會在最后進行任何包裝。 以上方法都不適用于數值型**Optional**。一般來說,流的`filter()`會在**Predicate**返回`false`時移除流元素。而`Optional.filter()`在失敗時不會刪除**Optional**,而是將其保留下來,并轉化為空。下面請看代碼示例: ~~~ // streams/OptionalFilter.java import java.util.*; import java.util.stream.*; import java.util.function.*; class OptionalFilter { static String[] elements = { "Foo", "", "Bar", "Baz", "Bingo" }; static Stream<String> testStream() { return Arrays.stream(elements); } static void test(String descr, Predicate<String> pred) { System.out.println(" ---( " + descr + " )---"); for(int i = 0; i <= elements.length; i++) { System.out.println( testStream() .skip(i) .findFirst() .filter(pred)); } } public static void main(String[] args) { test("true", str -> true); test("false", str -> false); test("str != \"\"", str -> str != ""); test("str.length() == 3", str -> str.length() == 3); test("startsWith(\"B\")", str -> str.startsWith("B")); } } ~~~ 輸出結果: ~~~ ---( true )--- Optional[Foo] Optional[] Optional[Bar] Optional[Baz] Optional[Bingo] Optional.empty ---( false )--- Optional.empty Optional.empty Optional.empty Optional.empty Optional.empty Optional.empty ---( str != "" )--- Optional[Foo] Optional.empty Optional[Bar] Optional[Baz] Optional[Bingo] Optional.empty ---( str.length() == 3 )--- Optional[Foo] Optional.empty Optional[Bar] Optional[Baz] Optional.empty Optional.empty ---( startsWith("B") )--- Optional.empty Optional.empty Optional[Bar] Optional[Baz] Optional[Bingo] Optional.empty ~~~ 即使輸出看起來像流,要特別注意`test()`中的 for 循環。每一次的for循環都重新啟動流,然后跳過for循環索引指定的數量的元素,這就是流只剩后續元素的原因。然后調用`findFirst()`獲取剩余元素中的第一個元素,并包裝在一個`Optional`對象中。 **注意**,不同于普通 for 循環,這里的索引值范圍并不是`i < elements.length`, 而是`i <= elements.length`。所以最后一個元素實際上超出了流。方便的是,這將自動成為**Optional.empty**,你可以在每一個測試的結尾中看到。 同`map()`一樣 ,`Optional.map()`執行一個函數。它僅在**Optional**不為空時才執行這個映射函數。并將**Optional**的內容提取出來,傳遞給映射函數。代碼示例: ~~~ // streams/OptionalMap.java import java.util.Arrays; import java.util.function.Function; import java.util.stream.Stream; class OptionalMap { static String[] elements = {"12", "", "23", "45"}; static Stream<String> testStream() { return Arrays.stream(elements); } static void test(String descr, Function<String, String> func) { System.out.println(" ---( " + descr + " )---"); for (int i = 0; i <= elements.length; i++) { System.out.println( testStream() .skip(i) .findFirst() // Produces an Optional .map(func)); } } public static void main(String[] args) { // If Optional is not empty, map() first extracts // the contents which it then passes // to the function: test("Add brackets", s -> "[" + s + "]"); test("Increment", s -> { try { return Integer.parseInt(s) + 1 + ""; } catch (NumberFormatException e) { return s; } }); test("Replace", s -> s.replace("2", "9")); test("Take last digit", s -> s.length() > 0 ? s.charAt(s.length() - 1) + "" : s); } // After the function is finished, map() wraps the // result in an Optional before returning it: } ~~~ 輸出結果: ~~~ ---( Add brackets )--- Optional[[12]] Optional[[]] Optional[[23]] Optional[[45]] Optional.empty ---( Increment )--- Optional[13] Optional[] Optional[24] Optional[46] Optional.empty ---( Replace )--- Optional[19] Optional[] Optional[93] Optional[45] Optional.empty ---( Take last digit )--- Optional[2] Optional[] Optional[3] Optional[5] Optional.empty ~~~ 映射函數的返回結果會自動包裝成為**Optional**。**Optional.empty**會被直接跳過。 **Optional**的`flatMap()`應用于已生成**Optional**的映射函數,所以`flatMap()`不會像`map()`那樣將結果封裝在**Optional**中。代碼示例: ~~~ // streams/OptionalFlatMap.java import java.util.Arrays; import java.util.Optional; import java.util.function.Function; import java.util.stream.Stream; class OptionalFlatMap { static String[] elements = {"12", "", "23", "45"}; static Stream<String> testStream() { return Arrays.stream(elements); } static void test(String descr, Function<String, Optional<String>> func) { System.out.println(" ---( " + descr + " )---"); for (int i = 0; i <= elements.length; i++) { System.out.println( testStream() .skip(i) .findFirst() .flatMap(func)); } } public static void main(String[] args) { test("Add brackets", s -> Optional.of("[" + s + "]")); test("Increment", s -> { try { return Optional.of( Integer.parseInt(s) + 1 + ""); } catch (NumberFormatException e) { return Optional.of(s); } }); test("Replace", s -> Optional.of(s.replace("2", "9"))); test("Take last digit", s -> Optional.of(s.length() > 0 ? s.charAt(s.length() - 1) + "" : s)); } } ~~~ 輸出結果: ~~~ ---( Add brackets )--- Optional[[12]] Optional[[]] Optional[[23]] Optional[[45]] Optional.empty ---( Increment )--- Optional[13] Optional[] Optional[24] Optional[46] Optional.empty ---( Replace )--- Optional[19] Optional[] Optional[93] Optional[45] Optional.empty ---( Take last digit )--- Optional[2] Optional[] Optional[3] Optional[5] Optional.empty ~~~ 同`map()`,`flatMap()`將提取非空**Optional**的內容并將其應用在映射函數。唯一的區別就是`flatMap()`不會把結果包裝在**Optional**中,因為映射函數已經被包裝過了。在如上示例中,我們已經在每一個映射函數中顯式地完成了包裝,但是很顯然`Optional.flatMap()`是為那些自己已經生成**Optional**的函數而設計的。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看