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

          Java8 Stream 一行代碼實(shí)現(xiàn)數(shù)據(jù)分組統(tǒng)計(jì)、排序、最大值、最小值、平均值、總數(shù)、合計(jì)

          共 10808字,需瀏覽 22分鐘

           ·

          2023-11-10 17:41

          胖虎和朋友原創(chuàng)的視頻教程有興趣的可以看看


          (文末附課程大綱)


          ??2024 最新,Java成神之路,架構(gòu)視頻(點(diǎn)擊查看)


          ??超全技術(shù)棧的Java入門+進(jìn)階+實(shí)戰(zhàn)!(點(diǎn)擊查看)

          來(lái)源:blog.csdn.net/xiaoheihai666/article/details/128152182

          • 示例:統(tǒng)計(jì)用戶status的最大值,最小值,求和,平均值
          • 分組統(tǒng)計(jì):
          • 如果我們想看某個(gè)部門下面有哪些數(shù)據(jù),可以如下代碼
          • 求最大值,最小值
          • 對(duì)某個(gè)字段求最大,最小,求和,統(tǒng)計(jì),計(jì)數(shù)
          • 求最大值,最小值還可以這樣做
          • 對(duì)某個(gè)字段求和并匯總
          • 求某個(gè)字段的平均值
          • 拼接某個(gè)字段的值,可以設(shè)置前綴,后綴或者分隔符
          • 根據(jù)部門進(jìn)行分組,并獲取匯總?cè)藬?shù)
          • 根據(jù)部門和是否退休進(jìn)行分組,并匯總?cè)藬?shù)
          • 根據(jù)部門和是否退休進(jìn)行分組,并取得每組中年齡最大的人

          Java8對(duì)數(shù)據(jù)處理可謂十分流暢,既不改變數(shù)據(jù),又能對(duì)數(shù)據(jù)進(jìn)行很好的處理,今天給大家演示下,用Java8的Stream如何對(duì)數(shù)據(jù)進(jìn)行分組統(tǒng)計(jì),排序,求和等

          這些方法屬于java 8的匯總統(tǒng)計(jì)類:

          • getAverage(): 它返回所有接受值的平均值。
          • getCount(): 它計(jì)算所有元素的總數(shù)。
          • getMax(): 它返回最大值。
          • getMin(): 它返回最小值。
          • getSum(): 它返回所有元素的總和。

          示例:統(tǒng)計(jì)用戶status的最大值,最小值,求和,平均值

          看官可以根據(jù)自己的需求進(jìn)行靈活變通

          @GetMapping("/list")
          public void list(){
              List<InputForm> inputForms = inputFormMapper.selectList();
              Map<String, IntSummaryStatistics> collect = inputForms.stream()
                  .collect(Collectors.groupingBy(InputForm::getCreateUserName, Collectors.summarizingInt(InputForm::getStatus)));

              // 對(duì)名字去重
              Set<String> collect1 = inputForms.stream().distinct().map(InputForm::getCreateUserName).collect(Collectors.toSet());

              // 遍歷名字,從map中取出對(duì)應(yīng)用戶的status最大值,最小值,平均值。。。
              for (String s1 : collect1) {
                  IntSummaryStatistics statistics1 = collect.get(s1);

                  System.out.println("第一個(gè)用戶的名字為====" + s1);
                  System.out.println("**********************************************");
                  System.out.println("status的個(gè)數(shù)為===" + statistics1.getCount());
                  System.out.println("status的最小值為===" + statistics1.getMin());
                  System.out.println("status的求和為===" + statistics1.getSum());
                  System.out.println("status的平均值為===" + statistics1.getAverage());
                  System.out.println();
                  System.out.println();
              }
          }

          結(jié)果如下:

          圖片

          分組統(tǒng)計(jì):

          @GetMapping("/list")
          public void list(){
              List<InputForm> inputForms = inputFormMapper.selectList();
              System.out.println("inputForms = " + inputForms);

              Map<String, Long> collect = inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateUserName,
                  Collectors.counting()));
              
              System.out.println("collect = " + collect);
          }

          ?

          其中Collectors.groupingBy(InputForm::getCreateUserName, Collectors.counting())返回的是一個(gè)Map集合,InputForm::getCreateUserName代表key,Collectors.counting()代表value,我是按照創(chuàng)建人的姓名進(jìn)行統(tǒng)計(jì)

          ?

          可以看到總共有九條數(shù)據(jù),其中莫昀錦有兩個(gè),周亞麗有七個(gè)

          2024最新架構(gòu)課程,對(duì)標(biāo)培訓(xùn)機(jī)構(gòu)

          ??點(diǎn)擊查看:Java成神之路-進(jìn)階架構(gòu)視頻!

          如果我們想看某個(gè)部門下面有哪些數(shù)據(jù),可以如下代碼

          @GetMapping("/list")
          public Map<String, List<InputForm>> list(){
              List<InputForm> inputForms = inputFormMapper.selectList();
              System.out.println("inputForms = " + inputForms);

              Map<String, List<InputForm>> collect = inputForms.stream()
                  .collect(Collectors.groupingBy(InputForm::getCreateCompanyName));

              return collect;
          }

          圖片

          求最大值,最小值

          @GetMapping("/list")
          public Map<String, List<InputForm>> list(){
              List<InputForm> inputForms = inputFormMapper.selectList();
              System.out.println("inputForms = " + inputForms);

              Optional<InputForm> min = inputForms.stream()
                  .min(Comparator.comparing(InputForm::getId));

              System.out.println("min = " + min);
              return null;
          }

          圖片

          可以看到此id是最小的,最大值雷同

          對(duì)某個(gè)字段求最大,最小,求和,統(tǒng)計(jì),計(jì)數(shù)

          @GetMapping("/list")
          public void list(){
              List<InputForm> inputForms = inputFormMapper.selectList();
              System.out.println("inputForms = " + inputForms);

              IntSummaryStatistics collect = inputForms.stream()
                  .collect(Collectors.summarizingInt(InputForm::getStatus));
              double average = collect.getAverage();
              int max = collect.getMax();
              int min = collect.getMin();
              long sum = collect.getSum();
              long count = collect.getCount();
              
              System.out.println("collect = " + collect);
          }

          求最大值,最小值還可以這樣做

          // 求最大值
          Optional<InputForm> max = inputForms.stream().max(Comparator.comparing(InputForm::getAgency));
          if (max.isPresent()){
              System.out.println("max = " + max);
          }

          // 求最小值
          Optional<InputForm> min = inputForms.stream().min(Comparator.comparing(InputForm::getAgency));
          if (min.isPresent()){
              System.out.println("min = " + min);
          }

          對(duì)某個(gè)字段求和并匯總

          int sum = inputForms.stream().mapToInt(InputForm::getStatus).sum();
                  System.out.println("sum = " + sum);

          求某個(gè)字段的平均值

          // 求某個(gè)字段的平均值
          Double collect2 = inputForms.stream().collect(Collectors.averagingInt(InputForm::getStatus));
          System.out.println("collect2 = " + collect2);

          // 簡(jiǎn)化后
          OptionalDouble average = inputForms.stream().mapToDouble(InputForm::getStatus).average();
          if (average.isPresent()){
              System.out.println("average = " + average);
          }

          拼接某個(gè)字段的值,可以設(shè)置前綴,后綴或者分隔符

          // 拼接某個(gè)字段的值,用逗號(hào)分隔,并設(shè)置前綴和后綴
          String collect3 = inputForms.stream().map(InputForm::getCreateUserName).collect(Collectors.joining(",""我是前綴""我是后綴"));
          System.out.println("collect3 = " + collect3);

          根據(jù)部門進(jìn)行分組,并獲取匯總?cè)藬?shù)

          // 根據(jù)部門進(jìn)行匯總,并獲取匯總?cè)藬?shù)
          Map<String, Long> collect4 = inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateDeptName, Collectors.counting()));
          System.out.println("collect4 = " + collect4);

          根據(jù)部門和是否退休進(jìn)行分組,并匯總?cè)藬?shù)

          // 根據(jù)部門和是否退休進(jìn)行分組,并匯總?cè)藬?shù)
          Map<String, Map<Integer, Long>> collect5 = inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateDeptName, Collectors.groupingBy(InputForm::getIsDelete, Collectors.counting())));
          System.out.println("collect5 = " + collect5);

          根據(jù)部門和是否退休進(jìn)行分組,并取得每組中年齡最大的人

          // 根據(jù)部門和是否退休進(jìn)行分組,并取得每組中年齡最大的人
          Map<String, Map<Integer, InputForm>> collect6 = inputForms.stream().collect(
              Collectors.groupingBy(InputForm::getCreateDeptName,
                  Collectors.groupingBy(InputForm::getIsDelete,
                      Collectors.collectingAndThen(
                          Collectors.maxBy(
                              Comparator.comparing(InputForm::getAge)), Optional::get))));
          System.out.println("collect6 = " + collect6);
               
               


                  
                  

          胖虎聯(lián)合兩位大佬朋友,一位是知名培訓(xùn)機(jī)構(gòu)講師和科大訊飛架構(gòu),聯(lián)合打造了《Java架構(gòu)師成長(zhǎng)之路》的視頻教程。完全對(duì)標(biāo)外面2萬(wàn)左右的培訓(xùn)課程。

          除了基本的視頻教程之外,還提供了超詳細(xì)的課堂筆記,以及源碼等資料包..


          課程階段:

          1. Java核心 提升閱讀源碼的內(nèi)功心法
          2. 深入講解企業(yè)開(kāi)發(fā)必備技術(shù)棧,夯實(shí)基礎(chǔ),為跳槽加薪增加籌碼
          3. 分布式架構(gòu)設(shè)計(jì)方法論。為學(xué)習(xí)分布式微服務(wù)做鋪墊
          4. 學(xué)習(xí)NetFilx公司產(chǎn)品,如Eureka、Hystrix、Zuul、Feign、Ribbon等,以及學(xué)習(xí)Spring Cloud Alibabba體系
          5. 微服務(wù)架構(gòu)下的性能優(yōu)化
          6. 中間件源碼剖析
          7. 元原生以及虛擬化技術(shù)
          8. 從0開(kāi)始,項(xiàng)目實(shí)戰(zhàn) SpringCloud Alibaba電商項(xiàng)目

          點(diǎn)擊下方超鏈接查看詳情(或者點(diǎn)擊文末閱讀原文):

          (點(diǎn)擊查看)  2024年,最新Java架構(gòu)師成長(zhǎng)之路 視頻教程!

          以下是課程大綱,大家可以雙擊打開(kāi)原圖查看

          瀏覽 1177
          點(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>
                  成人乱码一区二区三区 | 亚洲系列日韩 | 国产无码激情视频 | 日本岛国视频在线观看一区二区三区 | 大香蕉伊人 |