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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### [應用函數到元素](https://lingcoder.gitee.io/onjava8/#/book/14-Streams?id=%e5%ba%94%e7%94%a8%e5%87%bd%e6%95%b0%e5%88%b0%e5%85%83%e7%b4%a0) * `map(Function)`:將函數操作應用在輸入流的元素中,并將返回值傳遞到輸出流中。 * `mapToInt(ToIntFunction)`:操作同上,但結果是**IntStream**。 * `mapToLong(ToLongFunction)`:操作同上,但結果是**LongStream**。 * `mapToDouble(ToDoubleFunction)`:操作同上,但結果是**DoubleStream**。 在這里,我們使用`map()`映射多種函數到一個字符串流中。代碼示例: ~~~ // streams/FunctionMap.java import java.util.*; import java.util.stream.*; import java.util.function.*; class FunctionMap { 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 + " )---"); testStream() .map(func) .forEach(System.out::println); } public static void main(String[] args) { 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); } } ~~~ 輸出結果: ~~~ ---( add brackets )--- [12] [] [23] [45] ---( Increment )--- 13 24 46 ---( Replace )--- 19 93 45 ---( Take last digit )--- 2 3 5 ~~~ 在上面的自增示例中,我們用`Integer.parseInt()`嘗試將一個字符串轉化為整數。如果字符串不能被轉化成為整數就會拋出`NumberFormatException`異常,此時我們就回過頭來把原始字符串放到輸出流中。 在以上例子中,`map()`將一個字符串映射為另一個字符串,但是我們完全可以產生和接收類型完全不同的類型,從而改變流的數據類型。下面代碼示例: ~~~ // streams/FunctionMap2.java // Different input and output types (不同的輸入輸出類型) import java.util.*; import java.util.stream.*; class Numbered { final int n; Numbered(int n) { this.n = n; } @Override public String toString() { return "Numbered(" + n + ")"; } } class FunctionMap2 { public static void main(String[] args) { Stream.of(1, 5, 7, 9, 11, 13) .map(Numbered::new) .forEach(System.out::println); } } ~~~ 輸出結果: ~~~ Numbered(1) Numbered(5) Numbered(7) Numbered(9) Numbered(11) Numbered(13) ~~~ 我們將獲取到的整數通過構造器`Numbered::new`轉化成為`Numbered`類型。 如果使用**Function**返回的結果是數值類型的一種,我們必須使用合適的`mapTo數值類型`進行替代。代碼示例: ~~~ // streams/FunctionMap3.java // Producing numeric output streams( 產生數值輸出流) import java.util.*; import java.util.stream.*; class FunctionMap3 { public static void main(String[] args) { Stream.of("5", "7", "9") .mapToInt(Integer::parseInt) .forEach(n -> System.out.format("%d ", n)); System.out.println(); Stream.of("17", "19", "23") .mapToLong(Long::parseLong) .forEach(n -> System.out.format("%d ", n)); System.out.println(); Stream.of("17", "1.9", ".23") .mapToDouble(Double::parseDouble) .forEach(n -> System.out.format("%f ", n)); } } ~~~ 輸出結果: ~~~ 5 7 9 17 19 23 17.000000 1.900000 0.230000 ~~~ 遺憾的是,Java 設計者并沒有盡最大努力去消除基本類型。
                  <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>

                              哎呀哎呀视频在线观看