<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # :-: Stream API (上) # Stream API 上 ## 使用流 ### 創建流 在使用流之前,首先需要擁有一個數據源,并通過StreamAPI提供的一些方法獲取該數據源的流對象。數據源可以有多種形式: **1\. 集合** 這種數據源較為常用,通過stream()方法即可獲取流對象: ~~~ List<Person> list = new ArrayList<Person>(); Stream<Person> stream = list.stream(); ~~~ **2\. 數組** 通過Arrays類提供的靜態函數stream()獲取數組的流對象: ~~~ String[] names = {"chaimm","peter","john"}; Stream<String> stream = Arrays.stream(names); ~~~ **3\. 值** 直接將幾個值變成流對象: ~~~ Stream<String> stream = Stream.of("chaimm","peter","john"); ~~~ **4\. 文件** ~~~ try(Stream lines = Files.lines(Paths.get(“文件路徑名”),Charset.defaultCharset())){ //可對lines做一些操作 }catch(IOException e){ } ~~~ **5\. iterator** **創建無限流** ~~~ Stream.iterate(0, n -> n + 2) .limit(10) .forEach(System.out::println); ~~~ > PS:Java7簡化了IO操作,把打開IO操作放在try后的括號中即可省略關閉IO的代碼。 ### 篩選 filter filter 函數接收一個Lambda表達式作為參數,該表達式返回boolean,在執行過程中,流將元素逐一輸送給filter,并篩選出執行結果為true的元素。 如,篩選出所有學生: ~~~ List<Person> result = list.stream() .filter(Person::isStudent) .collect(toList()); ~~~ ### 去重distinct 去掉重復的結果: ~~~ List<Person> result = list.stream() .distinct() .collect(toList()); ~~~ ### 截取 截取流的前N個元素: ~~~ List<Person> result = list.stream() .limit(3) .collect(toList()); ~~~ ### 跳過 跳過流的前n個元素: ~~~ List<Person> result = list.stream() .skip(3) .collect(toList()); ~~~ ### 映射 對流中的每個元素執行一個函數,使得元素轉換成另一種類型輸出。流會將每一個元素輸送給map函數,并執行map中的Lambda表達式,最后將執行結果存入一個新的流中。 如,獲取每個人的姓名(實則是將Perosn類型轉換成String類型): ~~~ List<Person> result = list.stream() .map(Person::getName) .collect(toList()); ~~~ ### 合并多個流 例:列出List中各不相同的單詞,List集合如下: ~~~ List<String> list = new ArrayList<String>(); list.add("I am a boy"); list.add("I love the girl"); list.add("But the girl loves another girl"); ~~~ 思路如下: 首先將list變成流: ~~~ list.stream(); ~~~ 按空格分詞: ~~~ list.stream() .map(line->line.split(" ")); ~~~ 分完詞之后,每個元素變成了一個String\[\]數組。 將每個`String[]`變成流: ~~~ list.stream() .map(line->line.split(" ")) .map(Arrays::stream) ~~~ 此時一個大流里面包含了一個個小流,我們需要將這些小流合并成一個流。 將小流合并成一個大流:用`flatMap`替換剛才的 map ~~~ list.stream() .map(line->line.split(" ")) .flatMap(Arrays::stream) ~~~ 去重 ~~~ list.stream() .map(line->line.split(" ")) .flatMap(Arrays::stream) .distinct() .collect(toList()); ~~~ ### 是否匹配任一元素:anyMatch anyMatch用于判斷流中是否存在至少一個元素滿足指定的條件,這個判斷條件通過Lambda表達式傳遞給anyMatch,執行結果為boolean類型。 如,判斷list中是否有學生: ~~~ boolean result = list.stream() .anyMatch(Person::isStudent); ~~~ ### 是否匹配所有元素:allMatch allMatch用于判斷流中的所有元素是否都滿足指定條件,這個判斷條件通過Lambda表達式傳遞給anyMatch,執行結果為boolean類型。 如,判斷是否所有人都是學生: ~~~ boolean result = list.stream() .allMatch(Person::isStudent); ~~~ ### 是否未匹配所有元素:noneMatch noneMatch與allMatch恰恰相反,它用于判斷流中的所有元素是否都不滿足指定條件: ~~~ boolean result = list.stream() .noneMatch(Person::isStudent); ~~~ ### 獲取任一元素findAny findAny能夠從流中隨便選一個元素出來,它返回一個Optional類型的元素。 ~~~ Optional<Person> person = list.stream().findAny(); ~~~ ### 獲取第一個元素findFirst ~~~ Optional<Person> person = list.stream().findFirst(); ~~~ ### 歸約 歸約是將集合中的所有元素經過指定運算,折疊成一個元素輸出,如:求最值、平均數等,這些操作都是將一個集合的元素折疊成一個元素輸出。 在流中,reduce函數能實現歸約。 reduce函數接收兩個參數: 1. 初始值 2. 進行歸約操作的Lambda表達式 **元素求和:自定義Lambda表達式實現求和** 例:計算所有人的年齡總和 ~~~ int age = list.stream().reduce(0, (person1,person2)->person1.getAge()+person2.getAge()); ~~~ 1. reduce的第一個參數表示初試值為0; 2. reduce的第二個參數為需要進行的歸約操作,它接收一個擁有兩個參數的Lambda表達式,reduce會把流中的元素兩兩輸給Lambda表達式,最后將計算出累加之和。 **元素求和:使用Integer.sum函數求和** 上面的方法中我們自己定義了Lambda表達式實現求和運算,如果當前流的元素為數值類型,那么可以使用Integer提供了sum函數代替自定義的Lambda表達式,如: ~~~ int age = list.stream().reduce(0, Integer::sum); ~~~ Integer類還提供了`min`、`max`等一系列數值操作,當流中元素為數值類型時可以直接使用。 ### 數值流的使用 采用reduce進行數值操作會涉及到基本數值類型和引用數值類型之間的裝箱、拆箱操作,因此效率較低。 當流操作為純數值操作時,使用數值流能獲得較高的效率。 **將普通流轉換成數值流** StreamAPI提供了三種數值流:IntStream、DoubleStream、LongStream,也提供了將普通流轉換成數值流的三種方法:mapToInt、mapToDouble、mapToLong。 如,將Person中的age轉換成數值流: ~~~ IntStream stream = list.stream().mapToInt(Person::getAge); ~~~ **數值計算** 每種數值流都提供了數值計算函數,如max、min、sum等。如,找出最大的年齡: ~~~ OptionalInt maxAge = list.stream() .mapToInt(Person::getAge) .max(); ~~~ 由于數值流可能為空,并且給空的數值流計算最大值是沒有意義的,因此max函數返回OptionalInt,它是Optional的一個子類,能夠判斷流是否為空,并對流為空的情況作相應的處理。 此外,mapToInt、mapToDouble、mapToLong進行數值操作后的返回結果分別為:OptionalInt、OptionalDouble、OptionalLong ## 中間操作和收集操作 ![](https://img.kancloud.cn/9b/90/9b9000a0c1556b9c222e322153debc2d_873x617.png)
                  <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>

                              哎呀哎呀视频在线观看