亲宝软件园·资讯

展开

Java8进行多个字段分组统计实现 Java8进行多个字段分组统计的实例代码

胖子学习天地 人气:4

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

加载全部内容

相关教程
猜你喜欢
用户评论