<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 一行代碼實現(xiàn)數(shù)據(jù)分組統(tǒng)計、排序、最大值、最小值、平均值、總數(shù)、合計

          共 9171字,需瀏覽 19分鐘

           ·

          2023-11-02 03:28

          來源:blog.csdn.net/xiaoheihai666/article/details/128152182

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

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

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

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

          示例:統(tǒng)計用戶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)));

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

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

                  System.out.println("第一個用戶的名字為====" + s1);
                  System.out.println("**********************************************");
                  System.out.println("status的個數(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)計:

          @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())返回的是一個Map集合,InputForm::getCreateUserName代表key,Collectors.counting()代表value,我是按照創(chuàng)建人的姓名進(jìn)行統(tǒng)計

          ?

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

          如果我們想看某個部門下面有哪些數(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是最小的,最大值雷同

          對某個字段求最大,最小,求和,統(tǒng)計,計數(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);
          }

          對某個字段求和并匯總

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

          求某個字段的平均值

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

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

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

          // 拼接某個字段的值,用逗號分隔,并設(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);
               
               

          程序汪資料鏈接

          程序汪接的7個私活都在這里,經(jīng)驗整理

          Java項目分享  最新整理全集,找項目不累啦 07版

          堪稱神級的Spring Boot手冊,從基礎(chǔ)入門到實戰(zhàn)進(jìn)階

          臥槽!字節(jié)跳動《算法中文手冊》火了,完整版 PDF 開放下載!

          臥槽!阿里大佬總結(jié)的《圖解Java》火了,完整版PDF開放下載!

          字節(jié)跳動總結(jié)的設(shè)計模式 PDF 火了,完整版開放下載!

          歡迎添加程序汪個人微信 itwang007  進(jìn)粉絲群或圍觀朋友圈

          瀏覽 3022
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  国产,黄片,免费,在线看 | 色老板在线观看永久 | 后入视频网站 | 夜夜干天天 | 懂色av无码任你操久久久久蜜桃av |