91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

清丰县| 琼结县| 柘城县| 剑阁县| 砚山县| 北海市| 大悟县| 武平县| 丁青县| 阿坝县| 法库县| 辰溪县| 中西区| 桐庐县| 百色市| 昂仁县| 高邑县| 陵川县| 磐石市| 长兴县| 仪征市| 阜阳市| 金寨县| 获嘉县| 辽宁省| 新津县| 榕江县| 什邡市| 葫芦岛市| 芮城县| 永年县| 邯郸市| 中西区| 宾川县| 磐安县| 天气| 桂东县| 苏尼特右旗| 泰兴市| 巴青县| 永清县|