Java8进行多个字段分组统计的实例代码
程序员文章站
2022-06-15 14:30:25
java8进行多个字段分组统计实现代码如下:// 分组统计map countmap = records.stream().collect(collectors....
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进行多个字段分组统计的实例代码的文章就介绍到这了,更多相关java8进行多个字段分组统计实现内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
下一篇: php实现的简单多进程服务器类完整示例