温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java8如何进行多个字段分组统计

发布时间:2020-07-28 13:44:02 来源:亿速云 阅读:888 作者:小猪 栏目:编程语言

小编这次要给大家分享的是Java8如何进行多个字段分组统计,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

Java8进行多个字段分组统计实现代码如下:

// 分组统计
Map<String, Long> countMap = records.stream().collect(Collectors.groupingBy(o -> o.getProductType() + "_" + o.getCountry(), Collectors.counting()));
 
List<Record> countRecords = countMap.keySet().stream().map(key -> {
  String[] temp = key.split("_");
  String productType = temp[0];
  String country = temp[1];
   
  Record record = new Record();
  record.set("device_type", productType);
  record.set("location", country;
  record.set("count", countMap.get(key).intValue());
  return record;
}).collect(Collectors.toList());

实例补充

1.本实例其实可以用一句简单的sql去实现 由于我们项目数据库中时间存的是13位的时间戳 所以必须得转行成日期格式 才能进行分组 如下:

SELECT
    count(1) simpleNumber,
    SUM(penalty_amount) AS simplePenaltyAmount,
    Handling_department,
    create_time,
LEFT(FROM_UNIXTIME(LEFT(create_time,10)),7)
FROM
    t_case_simple_case
WHERE
    1 = 1
GROUP BY Handling_department,LEFT(FROM_UNIXTIME(LEFT(create_time,10)),7)

以上的就能实现多字段求和统计等功能 但我们老大考虑到查询速度和换库等问题 建议我不要用FROM_UNIXTIME()函数,所以只能用最笨的方法一步步实现 以下是用Java8方式实现的 代码如下:

@Override
public List<StatisticalAnalysis> queryFirstCase(String startTime,String condition,String caseType) {
  List<StatisticalAnalysis> statisticalAnalyses = null;
  //简易案件
  if (caseType.equals(CaseTypeEnum.SIMPLETYPE.code())) {
    statisticalAnalyses = statisticalAnalysisDao.querySimpleData();
  }
  //一般案件
  if (caseType.equals(CaseTypeEnum.NORMALTYPE.code())) {
    statisticalAnalyses = statisticalAnalysisDao.queryNormalData();
  }
  for (StatisticalAnalysis analysis : statisticalAnalyses) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
      String newCreateTime = sdf.format(new Date(Long.valueOf(analysis.getCreateTime())));
      analysis.setCreateTime(newCreateTime);
      SysOrg sysOrg = commonSearchDao.findByOrgId(analysis.getHandlingDepartment());
      if (sysOrg != null) {
        analysis.setHandlingDepartmentName(sysOrg.getOrgName());
      }
      if(analysis.getSimplePenaltyAmount()==null){
        analysis.setSimplePenaltyAmount(0.0);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  List<StatisticalAnalysis> caseTotalList = new ArrayList<>();
  //根据部门 日期进行分组 统计
  Map<String, Map<String, Long>> caseTotal = statisticalAnalyses.stream().collect(Collectors.groupingBy(StatisticalAnalysis::getCreateTime, Collectors.groupingBy(StatisticalAnalysis::getHandlingDepartmentName, Collectors.counting())));
  //根据部门 日期进行分组 求和
  Map<String, Map<String, Double>> sumCase = statisticalAnalyses.stream().collect(Collectors.groupingBy(StatisticalAnalysis::getCreateTime, Collectors.groupingBy(StatisticalAnalysis::getHandlingDepartmentName, Collectors.summingDouble(StatisticalAnalysis::getSimplePenaltyAmount))));
  //根据年份进行模糊匹配 取出对应的统计值
  for (Map.Entry<String, Map<String, Long>> entry : caseTotal.entrySet()) {
    if (entry.getKey().indexOf(startTime) > -1) {
      StatisticalAnalysis statisticalAnalysis = new StatisticalAnalysis();
      statisticalAnalysis.setCreateTime(entry.getKey());
      //将年份-去掉  组成新的数字  主要作为排序
      String key = entry.getKey().replace("-", "");
      Integer sortNum = Integer.parseInt(key);
      statisticalAnalysis.setSortNum(sortNum);
      Map<String, Long> map = entry.getValue();
      for (Map.Entry<String, Long> entr : map.entrySet()) {
        statisticalAnalysis.setHandlingDepartmentName(entr.getKey());
        statisticalAnalysis.setSimpleNumber(entr.getValue());
      }
      caseTotalList.add(statisticalAnalysis);
    }
  }
  caseTotalList.sort((a, b) -> a.getSortNum() - b.getSortNum());
  //根据年份进行模糊匹配 取出对应的求和值
  List<StatisticalAnalysis> analyses = new ArrayList<>();
  for (Map.Entry<String, Map<String, Double>> entry : sumCase.entrySet()) {
    if (entry.getKey().indexOf(startTime) > -1) {
      StatisticalAnalysis statisticalAnalysis = new StatisticalAnalysis();
      statisticalAnalysis.setCreateTime(entry.getKey());
      //将年份-去掉  组成新的数字  主要作为排序
      String key = entry.getKey().replace("-", "");
      Integer sortNum = Integer.parseInt(key);
      statisticalAnalysis.setSortNum(sortNum);
      Map<String, Double> map = entry.getValue();
      for (Map.Entry<String, Double> entr : map.entrySet()) {
        statisticalAnalysis.setHandlingDepartmentName(entr.getKey());
        statisticalAnalysis.setSimplePenaltyAmount(entr.getValue());
      }
      analyses.add(statisticalAnalysis);
    }
  }

  analyses.sort((a, b) -> a.getSortNum() - b.getSortNum());
  //将统计和求和组合成一个新的集合返回前端
  List<StatisticalAnalysis> analysisList = new ArrayList<>();
  for (StatisticalAnalysis analys : caseTotalList) {
    for (StatisticalAnalysis analys2 : analyses) {
      StatisticalAnalysis statisticalAnalysis = new StatisticalAnalysis();
      if (analys.getSortNum().intValue() == analys2.getSortNum().intValue()) {
        statisticalAnalysis.setSimpleNumber(analys.getSimpleNumber());
        statisticalAnalysis.setSimplePenaltyAmount(analys2.getSimplePenaltyAmount());
        statisticalAnalysis.setCreateTime(analys.getCreateTime());
        statisticalAnalysis.setHandlingDepartmentName(analys.getHandlingDepartmentName());
        analysisList.add(statisticalAnalysis);
      }
    }
  }
  List<StatisticalAnalysis> newAnalysisList = new ArrayList<>();
  if (analysisList.size() > 0) {
    //查询第一季度
    if (condition.equals(YearEnum.FIRST.code())) {
      for (StatisticalAnalysis analysis : analysisList) {
        StatisticalAnalysis analysis1 = new StatisticalAnalysis();
        if (analysis.getCreateTime().indexOf(startTime + "-01") > -1) {
          BeanUtils.copyProperties(analysis, analysis1);
          if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
            analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"各部门简易案件");
          }else{
            analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"各部门一般案件");
          }
          newAnalysisList.add(analysis1);
        }
        if (analysis.getCreateTime().indexOf(startTime + "-02") > -1) {
          BeanUtils.copyProperties(analysis, analysis1);
          if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
            analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"各部门简易案件");
          }else{
            analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"各部门一般案件");
          }
          newAnalysisList.add(analysis1);
        }
        if (analysis.getCreateTime().indexOf(startTime + "-03") > -1) {
          BeanUtils.copyProperties(analysis, analysis1);
          if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
            analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"各部门简易案件");
          }else{
            analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"各部门一般案件");
          }
          newAnalysisList.add(analysis1);
        }
      }
      return newAnalysisList;
  }
  //查询第二季度
  if (condition.equals(YearEnum.SECOND.code())) {
    for (StatisticalAnalysis analysis : analysisList) {
      StatisticalAnalysis analysis1 = new StatisticalAnalysis();
      if (analysis.getCreateTime().indexOf(startTime + "-04") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-05") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-06") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
    }
    return newAnalysisList;
  }
  //查询第三季度
  if (condition.equals(YearEnum.THREE.code())) {
    for (StatisticalAnalysis analysis : analysisList) {
      StatisticalAnalysis analysis1 = new StatisticalAnalysis();
      if (analysis.getCreateTime().indexOf(startTime + "-07") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.THREE.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.THREE.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-08") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.THREE.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.THREE.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-09") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.THREE.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.THREE.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
    }
    return newAnalysisList;
  }
  //查询第四季度
  if (condition.equals(YearEnum.FOUR.code())) {
    for (StatisticalAnalysis analysis : analysisList) {
      StatisticalAnalysis analysis1 = new StatisticalAnalysis();
      if (analysis.getCreateTime().indexOf(startTime + "-10") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-11") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-12") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
    }
    return newAnalysisList;
  }
  //查询上半年 取前六条数据
  if (condition.equals(YearEnum.HALF.code())) {
    for (StatisticalAnalysis analysis : analysisList) {
      StatisticalAnalysis analysis1 = new StatisticalAnalysis();
      if (analysis.getCreateTime().indexOf(startTime + "-01") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-02") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-03") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-04") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-05") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-06") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.HALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
    }
    return newAnalysisList;
  }
  //查询下半年 后六条数据
  if (condition.equals(YearEnum.LASTHALF.code())) {
    for (StatisticalAnalysis analysis : analysisList) {
      StatisticalAnalysis analysis1 = new StatisticalAnalysis();
      if (analysis.getCreateTime().indexOf(startTime + "-07") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-08") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-09") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-10") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-11") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
      if (analysis.getCreateTime().indexOf(startTime + "-12") > -1) {
        BeanUtils.copyProperties(analysis, analysis1);
        if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门简易案件");
        }else{
          analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"各部门一般案件");
        }
        newAnalysisList.add(analysis1);
      }
    }
    return newAnalysisList;
  }
  if (condition.equals(YearEnum.FULLYEAR.code())) {
    for (StatisticalAnalysis analysis : analysisList) {
      if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
        analysis.setTitle(startTime+YearEnum.FULLYEAR.desc()+"各部门简易案件");
      }else{
        analysis.setTitle(startTime+YearEnum.FULLYEAR.desc()+"各部门一般案件");
      }
    }
    return analysisList;
  }
}
  return null;
}

看完这篇关于Java8如何进行多个字段分组统计的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI