<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Collectors.reducing總結(jié)

          共 1935字,需瀏覽 4分鐘

           ·

          2021-08-31 08:45


          1. 方法簽名 一個(gè)參數(shù)

          public?static?<T>?Collector<T,??,?Optional<T>>?reducing(BinaryOperator<T>?op)

          參數(shù)說明

          • BinaryOperatorop 歸集操作函數(shù) 輸入?yún)?shù)T返回T

          測(cè)試代碼

          我們這里實(shí)現(xiàn)一個(gè)簡(jiǎn)單的求和功能,代碼如下

          //author: herbert 公眾號(hào):小院不小 20210827
          ?@Test
          ?public?void?testReducingOne()?{
          ??List<Integer>?testData?=?Arrays.asList(1,?2,?3,?4,?5,?6,?7,?8,?9);
          ??Optional<Integer>?sum?=?testData.stream().collect(Collectors.reducing((prev,?cur)?->?{
          ???System.out.println("prev=>"?+?prev?+?"cur=>"?+?cur);
          ???return?prev?+?cur;
          ??}));
          ??System.out.print(sum.get());?//?45?
          ?}

          2. 方法簽名 兩個(gè)參數(shù)

          public?static?<T>?Collector<T,??,?T>?reducing(T?identity,?BinaryOperator<T>?op)

          參數(shù)說明

          • T identity 返回類型T初始值
          • BinaryOperatorop 歸集操作函數(shù) 輸入?yún)?shù)T返回T

          測(cè)試代碼

          我們這里實(shí)現(xiàn)一個(gè)簡(jiǎn)單的求和并加上20功能,代碼如下

          //author: herbert 公眾號(hào):小院不小 20210827
          ?@Test
          ?public?void?testReducingTwo()?{
          ??List<Integer>?testData?=?Arrays.asList(1,?2,?3,?4,?5,?6,?7,?8,?9);
          ??Integer?sum?=?testData.stream().collect(Collectors.reducing(20,?(prev,?cur)?->?{
          ???System.out.println("prev=>"?+?prev?+?"cur=>"?+?cur);
          ???return?prev?+?cur;
          ??}));
          ??System.out.print(sum);?//65
          ?}

          2. 方法簽名 三個(gè)參數(shù)

          public?static?<T,?U>?Collector<T,??,?U>?reducing(U?identity,Function<??super?T,???extends?U>?mapper,BinaryOperator<U>?op)

          這個(gè)函數(shù)才是真正體現(xiàn)reducing(歸集)的過程。調(diào)用者要明確知道以下三個(gè)點(diǎn)

          1. 需要轉(zhuǎn)換類型的初始值
          2. 類型如何轉(zhuǎn)換
          3. 如何收集返回值

          參數(shù)說明

          • U identity 最終返回類型U初始值
          • Function<? super T, ? extends U> mapper 將輸入?yún)?shù)T轉(zhuǎn)換成返回類型U的函數(shù)
          • BinaryOperator<U> op 歸集操作函數(shù) 輸入?yún)?shù)U返回U

          測(cè)試代碼

          我們這里實(shí)現(xiàn)一個(gè)簡(jiǎn)單數(shù)字轉(zhuǎn)字符串并按逗號(hào)連接的功能,代碼如下

          //author: herbert 公眾號(hào):小院不小 20210827
          ?@Test
          ?public?void?testReducingThree()?{
          ??List<Integer>?testData?=?Arrays.asList(1,?2,?3,?4,?5,?6,?7,?8,?9);
          ??String?joinStr?=?testData.stream().collect(Collectors.reducing("轉(zhuǎn)換成字符串",?in?->?{
          ???return?in?+?"";
          ??},?(perv,?cur)?->?{
          ???return?perv?+?","?+?cur;
          ??}));
          ??System.out.print(joinStr);?//?轉(zhuǎn)換成字符串,1,2,3,4,5,6,7,8,9
          ?}

          4. 總結(jié)

          這個(gè)知識(shí)點(diǎn)很小,但在沒有徹底明白之前,對(duì)三個(gè)參數(shù)的調(diào)用特別糊涂。最主要的原因就是看到一堆 T R U 的泛型類型就不知道如何下手。歡迎大家關(guān)注我的公眾號(hào)一起收集開發(fā)中遇到的點(diǎn)滴知識(shí)

          瀏覽 51
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩高清中文字幕 | 日韩综合欧美a在线 | 亚洲一极黄色片 | 麻豆精品一区 | 亚洲无码专区区免费 |