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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                #### Stream * 流不存儲元素;它們存儲在底層的集合或者按需生成; * 流操作不改變它們的源數據;例如filter方法不會從一個新流中刪除元素,而是生成一個不包含特定元素的新流; * Stream操作可能是延遲執行的; * 和迭代器類似,流只能遍歷一次 * filter和map等操作是無狀態的,它們并不存儲任何狀態。reduce等操作要存儲狀態才能計算出一個值。sorted和distinct等操作也要存儲狀態,因為它們需要把流中的所有元素緩存起來才能返回一個新的流。這種操作稱為有狀態操作 ### 支持兩種類型操作: * 中間操作(如filter或map) * 終端操作(如count、findFirst、forEach和reduce) ### 常用API * IntStream.rangeClosed\(1, 100\):rangeClosed方法來生成1到100之間的所有數字 * Stream.of:顯式值創建一個流 * Stream.empty\(\):創建一個空流 * Arrays.stream\(numbers\):從數組創建一個流 ### 外部迭代與內部迭代 使用Collection接口需要用戶去做迭代(比如用for-each),這稱為外部迭代 Streams庫使用內部迭代——它幫你把迭代做了,還把得到的流值存在了某個地方,你只要給出一個函數說要干什么就可以了 #### API | Stream操作 | 描述 | | --- | --- | | filter | filter轉換生成一個匹配一定條件的新流過濾 | | mapToXXX | 使用toXXXFunction對流中的元素執行一對一的轉換,該方法返回的新流中包含了toXXXFunction轉換生成的所有元素; | | forEach | 迭代 | | count | 統計 | | map | 將流中的值進行某種轉換 | | reduce | 歸約操作 | | generate | 接受一個無參數的函數 | | iterate | iterate方法接受一個"種子"值和一個函數并對之前的值重復應用該函數 | | limit | 返回一個包含n個元素的新流 | | skip | 跳過或丟棄前n個元素 | | toArray | 返回一個數組 | | collect | 收集 | | concat | 連接兩個流 | | distinct | 過濾重復元素 | | sorted | 排序 | | peek | 每當檢索元素的時候就會調用一次\(可用來調試流\) | | max | 取最大值 | | min | 取最小值 | | findFirst | 返回非空集合中的第一個值 | | findAny | 返回任何一個匹配的元素 | | anyMatch | 返回是否含有匹配元素 | | flatMap | flatmap方法讓你把一個流中的每個值都換成另一個流,然后把所有的流連接起來成為一個流 | #### Method ``` Stream<String> stream = Stream.of("1,2,3,4,10".split(",")); Arrays.stream("h,2,34,3".split(",")).forEach(System.err::println); // Stream<Double> stm = Stream.generate(Math::random).limit(10); // 如果沒有limit流會一直運行 Stream<String> stm = Stream.generate(() -> "hello").limit(10); Pattern.compile("\\PL+").splitAsStream("hello,world").forEach(System.err::println); ``` #### CodeList ``` Runnable run = () -> System.out.println("running...”); ##不帶參數 ActionListener action = event -> System.err.println("button click”); ##帶參數 Runnable run = () -> { ##代碼塊 System.out.println("running..."); System.err.println("end"); return; }; IntSummaryStatistics summaryStatistics = list.stream().mapToInt(e -> e.intValue()).summaryStatistics(); //統計 方法引用:System.out::println(語法:Classname::method) ``` ![](https://img.kancloud.cn/55/3a/553ab362fcb6d3769270c757de28178e_613x173.png) ### 常見用法 ``` // 打印輸出 list.stream().forEach(System.err::println); // 過濾 list.stream().filter(t -> t > 50).forEach(System.out::println); // 排序 list.stream().sorted((x1, x2) -> x2.compareTo(x1)).forEach(System.err::println); // 求總記錄數 System.err.println(list.stream().count()); // 轉換為Map Map<String, TerraOrderBill> billMap = orderBillList.stream().collect( Collectors.toMap(TerraOrderBill::getBillId,bill -> bill)); // 求和 double totalAmount = seEntries.stream().mapToDouble(t -> t.getRealPayAmount()).sum(); // distinct recordEntrys.stream().filter(t -> !StringUtils.isEmpty(t.getSupplierId())) .map(SettlementRecordEntry::getSupplierId).distinct().collect(Collectors.toList()); // 并行處理 sum = list.parallelStream().mapToInt(t -> t.intValue()).sum(); ```
                  <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>

                              哎呀哎呀视频在线观看