重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
小编这次要给大家分享的是Java8如何进行多个字段分组统计,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
创新互联公司长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为剑川企业提供专业的成都网站设计、网站制作,剑川网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
Java8进行多个字段分组统计实现代码如下:
// 分组统计 MapcountMap = records.stream().collect(Collectors.groupingBy(o -> o.getProductType() + "_" + o.getCountry(), Collectors.counting())); List 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 ListqueryFirstCase(String startTime,String condition,String caseType) { List 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 caseTotalList = new ArrayList<>(); //根据部门 日期进行分组 统计 Map > caseTotal = statisticalAnalyses.stream().collect(Collectors.groupingBy(StatisticalAnalysis::getCreateTime, Collectors.groupingBy(StatisticalAnalysis::getHandlingDepartmentName, Collectors.counting()))); //根据部门 日期进行分组 求和 Map > sumCase = statisticalAnalyses.stream().collect(Collectors.groupingBy(StatisticalAnalysis::getCreateTime, Collectors.groupingBy(StatisticalAnalysis::getHandlingDepartmentName, Collectors.summingDouble(StatisticalAnalysis::getSimplePenaltyAmount)))); //根据年份进行模糊匹配 取出对应的统计值 for (Map.Entry > 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 map = entry.getValue(); for (Map.Entry entr : map.entrySet()) { statisticalAnalysis.setHandlingDepartmentName(entr.getKey()); statisticalAnalysis.setSimpleNumber(entr.getValue()); } caseTotalList.add(statisticalAnalysis); } } caseTotalList.sort((a, b) -> a.getSortNum() - b.getSortNum()); //根据年份进行模糊匹配 取出对应的求和值 List analyses = new ArrayList<>(); for (Map.Entry > 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 map = entry.getValue(); for (Map.Entry entr : map.entrySet()) { statisticalAnalysis.setHandlingDepartmentName(entr.getKey()); statisticalAnalysis.setSimplePenaltyAmount(entr.getValue()); } analyses.add(statisticalAnalysis); } } analyses.sort((a, b) -> a.getSortNum() - b.getSortNum()); //将统计和求和组合成一个新的集合返回前端 List 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 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如何进行多个字段分组统计的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。