您好,登錄后才能下訂單哦!
小編這次要給大家分享的是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如何進行多個字段分組統計的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。