Java8进行多个字段分组统计的实例代码
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("_"); StringproductType=temp[0]; Stringcountry=temp[1]; Recordrecord=newRecord(); record.set("device_type",productType); record.set("location",country; record.set("count",countMap.get(key).intValue()); returnrecord; }).collect(Collectors.toList());
实例补充:
1.本实例其实可以用一句简单的sql去实现由于我们项目数据库中时间存的是13位的时间戳所以必须得转行成日期格式才能进行分组如下:
SELECT count(1)simpleNumber, SUM(penalty_amount)ASsimplePenaltyAmount, Handling_department, create_time, LEFT(FROM_UNIXTIME(LEFT(create_time,10)),7) FROM t_case_simple_case WHERE 1=1 GROUPBYHandling_department,LEFT(FROM_UNIXTIME(LEFT(create_time,10)),7)
以上的就能实现多字段求和统计等功能但我们老大考虑到查询速度和换库等问题建议我不要用FROM_UNIXTIME()函数,所以只能用最笨的方法一步步实现以下是用Java8方式实现的代码如下:
@Override publicListqueryFirstCase(StringstartTime,Stringcondition,StringcaseType){ List statisticalAnalyses=null; //简易案件 if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){ statisticalAnalyses=statisticalAnalysisDao.querySimpleData(); } //一般案件 if(caseType.equals(CaseTypeEnum.NORMALTYPE.code())){ statisticalAnalyses=statisticalAnalysisDao.queryNormalData(); } for(StatisticalAnalysisanalysis:statisticalAnalyses){ try{ SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM"); StringnewCreateTime=sdf.format(newDate(Long.valueOf(analysis.getCreateTime()))); analysis.setCreateTime(newCreateTime); SysOrgsysOrg=commonSearchDao.findByOrgId(analysis.getHandlingDepartment()); if(sysOrg!=null){ analysis.setHandlingDepartmentName(sysOrg.getOrgName()); } if(analysis.getSimplePenaltyAmount()==null){ analysis.setSimplePenaltyAmount(0.0); } }catch(Exceptione){ e.printStackTrace(); } } List caseTotalList=newArrayList<>(); //根据部门日期进行分组统计 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){ StatisticalAnalysisstatisticalAnalysis=newStatisticalAnalysis(); statisticalAnalysis.setCreateTime(entry.getKey()); //将年份-去掉组成新的数字主要作为排序 Stringkey=entry.getKey().replace("-",""); IntegersortNum=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=newArrayList<>(); for(Map.Entry >entry:sumCase.entrySet()){ if(entry.getKey().indexOf(startTime)>-1){ StatisticalAnalysisstatisticalAnalysis=newStatisticalAnalysis(); statisticalAnalysis.setCreateTime(entry.getKey()); //将年份-去掉组成新的数字主要作为排序 Stringkey=entry.getKey().replace("-",""); IntegersortNum=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=newArrayList<>(); for(StatisticalAnalysisanalys:caseTotalList){ for(StatisticalAnalysisanalys2:analyses){ StatisticalAnalysisstatisticalAnalysis=newStatisticalAnalysis(); 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=newArrayList<>(); if(analysisList.size()>0){ //查询第一季度 if(condition.equals(YearEnum.FIRST.code())){ for(StatisticalAnalysisanalysis:analysisList){ StatisticalAnalysisanalysis1=newStatisticalAnalysis(); 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); } } returnnewAnalysisList; } //查询第二季度 if(condition.equals(YearEnum.SECOND.code())){ for(StatisticalAnalysisanalysis:analysisList){ StatisticalAnalysisanalysis1=newStatisticalAnalysis(); 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); } } returnnewAnalysisList; } //查询第三季度 if(condition.equals(YearEnum.THREE.code())){ for(StatisticalAnalysisanalysis:analysisList){ StatisticalAnalysisanalysis1=newStatisticalAnalysis(); 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); } } returnnewAnalysisList; } //查询第四季度 if(condition.equals(YearEnum.FOUR.code())){ for(StatisticalAnalysisanalysis:analysisList){ StatisticalAnalysisanalysis1=newStatisticalAnalysis(); 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); } } returnnewAnalysisList; } //查询上半年取前六条数据 if(condition.equals(YearEnum.HALF.code())){ for(StatisticalAnalysisanalysis:analysisList){ StatisticalAnalysisanalysis1=newStatisticalAnalysis(); 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); } } returnnewAnalysisList; } //查询下半年后六条数据 if(condition.equals(YearEnum.LASTHALF.code())){ for(StatisticalAnalysisanalysis:analysisList){ StatisticalAnalysisanalysis1=newStatisticalAnalysis(); 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); } } returnnewAnalysisList; } if(condition.equals(YearEnum.FULLYEAR.code())){ for(StatisticalAnalysisanalysis:analysisList){ if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){ analysis.setTitle(startTime+YearEnum.FULLYEAR.desc()+"各部门简易案件"); }else{ analysis.setTitle(startTime+YearEnum.FULLYEAR.desc()+"各部门一般案件"); } } returnanalysisList; } } returnnull; }
到此这篇关于Java8进行多个字段分组统计的实例代码的文章就介绍到这了,更多相关Java8进行多个字段分组统计实现内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!