中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java8如何進行多個字段分組統計

發布時間:2020-07-28 13:44:02 來源:億速云 閱讀:878 作者:小豬 欄目:編程語言

小編這次要給大家分享的是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

鹤庆县| 苍南县| 谷城县| 德兴市| 织金县| 郁南县| 麻江县| 临邑县| 东丰县| 二连浩特市| 武汉市| 合肥市| 灵石县| 黄浦区| 镇雄县| 十堰市| 绩溪县| 米脂县| 瑞金市| 武威市| 和平县| 左贡县| 蒲城县| 永德县| 石屏县| 宜昌市| 阿拉善右旗| 南通市| 海淀区| 岚皋县| 大田县| 巴青县| 衢州市| 仲巴县| 洞头县| 明星| 长葛市| 衡水市| 西林县| 喀喇沁旗| 吴桥县|