Java8 Stream针对List先分组再求和、最大值、最小值、平均值等

发布时间 2023-07-21 16:01:42作者: 甜菜波波

解题思路JAVA8使用stream()根据类型对List进行分组统计。

核心功能代码片段

  1.  
    //分组求和
  2.  
    Map<String, LongSummaryStatistics> collect = list.stream().collect(
  3.  
    Collectors.groupingBy(Fruit::getType,
  4.  
    Collectors.summarizingLong(Fruit::getTotal)));
  5.  
    for (Map.Entry<String, LongSummaryStatistics> entry : collect.entrySet()) {
  6.  
    LongSummaryStatistics longSummaryStatistics = entry.getValue();
  7.  
    System.out.println("----------------key----------------" + entry.getKey());
  8.  
    System.out.println("求和:" + longSummaryStatistics.getSum());
  9.  
    System.out.println("求平均" + longSummaryStatistics.getAverage());
  10.  
    System.out.println("求最大:" + longSummaryStatistics.getMax());
  11.  
    System.out.println("求最小:" + longSummaryStatistics.getMin());
  12.  
    System.out.println("求总数:" + longSummaryStatistics.getCount());
  13.  
    }

演示功能代码

  1.  
    package com.zzg.test;
  2.  
     
  3.  
    import java.math.BigDecimal;
  4.  
    import java.util.ArrayList;
  5.  
    import java.util.List;
  6.  
    import java.util.LongSummaryStatistics;
  7.  
    import java.util.Map;
  8.  
    import java.util.stream.Collectors;
  9.  
     
  10.  
    import cn.hutool.json.JSONUtil;
  11.  
     
  12.  
    /**
  13.  
    * 基于Java8 分组再统计
  14.  
    * @author zzg
  15.  
    *
  16.  
    */
  17.  
    public class GroupByStatissticsTest {
  18.  
     
  19.  
    static List<Fruit> initDate(){
  20.  
    List<Fruit> list = new ArrayList<Fruit>();
  21.  
     
  22.  
    Fruit one = new Fruit();
  23.  
    one.setName("苹果一级");
  24.  
    one.setSid("1");
  25.  
    one.setPrice(new BigDecimal("123456.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  26.  
    one.setTotal(1100L);
  27.  
    one.setType("1");
  28.  
     
  29.  
     
  30.  
    Fruit two = new Fruit();
  31.  
    two.setName("苹果二级");
  32.  
    two.setSid("2");
  33.  
    two.setPrice(new BigDecimal("123546.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  34.  
    two.setTotal(89L);
  35.  
    two.setType("1");
  36.  
     
  37.  
    Fruit three = new Fruit();
  38.  
    three.setName("苹果三级");
  39.  
    three.setSid("3");
  40.  
    three.setPrice(new BigDecimal("987.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  41.  
    three.setTotal(1039L);
  42.  
    three.setType("1");
  43.  
     
  44.  
    Fruit four = new Fruit();
  45.  
    four.setName("梨子一级");
  46.  
    four.setSid("4");
  47.  
    four.setPrice(new BigDecimal("97.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  48.  
    four.setTotal(39L);
  49.  
    four.setType("2");
  50.  
     
  51.  
    Fruit five = new Fruit();
  52.  
    five.setName("梨子二级");
  53.  
    five.setSid("5");
  54.  
    five.setPrice(new BigDecimal("970.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  55.  
    five.setTotal(399L);
  56.  
    five.setType("2");
  57.  
     
  58.  
    Fruit six = new Fruit();
  59.  
    six.setName("西瓜一级");
  60.  
    six.setSid("6");
  61.  
    six.setPrice(new BigDecimal("1970.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
  62.  
    six.setTotal(2399L);
  63.  
    six.setType("3");
  64.  
     
  65.  
    list.add(one);
  66.  
    list.add(two);
  67.  
    list.add(three);
  68.  
    list.add(four);
  69.  
    list.add(five);
  70.  
    list.add(six);
  71.  
    return list;
  72.  
    }
  73.  
     
  74.  
    public static void main(String[] args) {
  75.  
    // TODO Auto-generated method stub
  76.  
    List<Fruit> list = initDate();
  77.  
     
  78.  
    //分组
  79.  
    Map<String,List<Fruit>> map = list.stream().collect(Collectors.groupingBy(Fruit::getType));
  80.  
    for (Map.Entry<String, List<Fruit>> entry : map.entrySet()) {
  81.  
    System.out.println("分组" + JSONUtil.toJsonStr(entry));
  82.  
    }
  83.  
     
  84.  
    //分组求和
  85.  
    Map<String, LongSummaryStatistics> collect = list.stream().collect(
  86.  
    Collectors.groupingBy(Fruit::getType,
  87.  
    Collectors.summarizingLong(Fruit::getTotal)));
  88.  
    for (Map.Entry<String, LongSummaryStatistics> entry : collect.entrySet()) {
  89.  
    LongSummaryStatistics longSummaryStatistics = entry.getValue();
  90.  
    System.out.println("----------------key----------------" + entry.getKey());
  91.  
    System.out.println("求和:" + longSummaryStatistics.getSum());
  92.  
    System.out.println("求平均" + longSummaryStatistics.getAverage());
  93.  
    System.out.println("求最大:" + longSummaryStatistics.getMax());
  94.  
    System.out.println("求最小:" + longSummaryStatistics.getMin());
  95.  
    System.out.println("求总数:" + longSummaryStatistics.getCount());
  96.  
    }
  97.  
     
  98.  
     
  99.  
    }
  100.  
     
  101.  
    static class Fruit{
  102.  
    private String sid;
  103.  
    private String name;
  104.  
    private String type;
  105.  
    private Long total;
  106.  
    private BigDecimal price;
  107.  
    public String getSid() {
  108.  
    return sid;
  109.  
    }
  110.  
    public void setSid(String sid) {
  111.  
    this.sid = sid;
  112.  
    }
  113.  
    public String getName() {
  114.  
    return name;
  115.  
    }
  116.  
    public void setName(String name) {
  117.  
    this.name = name;
  118.  
    }
  119.  
    public String getType() {
  120.  
    return type;
  121.  
    }
  122.  
    public void setType(String type) {
  123.  
    this.type = type;
  124.  
    }
  125.  
    public Long getTotal() {
  126.  
    return total;
  127.  
    }
  128.  
    public void setTotal(Long total) {
  129.  
    this.total = total;
  130.  
    }
  131.  
    public BigDecimal getPrice() {
  132.  
    return price;
  133.  
    }
  134.  
    public void setPrice(BigDecimal price) {
  135.  
    this.price = price;
  136.  
    }
  137.  
     
  138.  
     
  139.  
     
  140.  
    }
  141.  
     
  142.  
    }

效果截图

 

转自:https://blog.csdn.net/zhouzhiwengang/article/details/123558048