<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ### [查找](https://lingcoder.gitee.io/onjava8/#/book/14-Streams?id=%e6%9f%a5%e6%89%be) * `findFirst()`:返回第一個流元素的**Optional**,如果流為空返回**Optional.empty**。 * `findAny(`:返回含有任意流元素的**Optional**,如果流為空返回**Optional.empty**。 代碼示例: ~~~ // streams/SelectElement.java import java.util.*; import java.util.stream.*; import static streams.RandInts.*; public class SelectElement { public static void main(String[] args) { System.out.println(rands().findFirst().getAsInt()); System.out.println( rands().parallel().findFirst().getAsInt()); System.out.println(rands().findAny().getAsInt()); System.out.println( rands().parallel().findAny().getAsInt()); } } ~~~ 輸出結果: ~~~ 258 258 258 242 ~~~ 無論流是否為并行化,`findFirst()`總是會選擇流中的第一個元素。對于非并行流,`findAny()`會選擇流中的第一個元素(即使從定義上來看是選擇任意元素)。在這個例子中,用`parallel()`將流并行化,以展示`findAny()`不選擇流的第一個元素的可能性。 如果必須選擇流中最后一個元素,那就使用`reduce()`。代碼示例: ~~~ // streams/LastElement.java import java.util.*; import java.util.stream.*; public class LastElement { public static void main(String[] args) { OptionalInt last = IntStream.range(10, 20) .reduce((n1, n2) -> n2); System.out.println(last.orElse(-1)); // Non-numeric object: Optional<String> lastobj = Stream.of("one", "two", "three") .reduce((n1, n2) -> n2); System.out.println( lastobj.orElse("Nothing there!")); } } ~~~ 輸出結果: ~~~ 19 three ~~~ `reduce()`的參數只是用最后一個元素替換了最后兩個元素,最終只生成最后一個元素。如果為數字流,你必須使用相近的數字**Optional**類型( numeric optional type),否則使用**Optional**類型,就像上例中的`Optional<String>`。
                  <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>

                              哎呀哎呀视频在线观看