两个map中的数据,按照相同键,将所对应的值相加方法
大家好,我是雄雄,歡迎關(guān)注微信公眾號:雄雄的小課堂
”前言
最近寫代碼的時候遇到了個需求,剛開始想的時候,感覺不難,挺簡單的,結(jié)果寫的時候,各種思考、各種費(fèi)腦,耗費(fèi)了點(diǎn)時間,終于實(shí)現(xiàn)了。
需求:因?yàn)橐恍┰?#xff0c;數(shù)據(jù)庫中有兩個表,結(jié)構(gòu)一樣且數(shù)據(jù)不一樣,現(xiàn)在需要對這兩個表進(jìn)行按照小時分組統(tǒng)計(jì)數(shù)據(jù),然后展示在前臺。
效果如下:
需要將兩個表中各個時間段的數(shù)據(jù)相加,然后重新封裝返回到前臺做展示。
思路分析
分別按照小時分組查詢兩個表中的數(shù)據(jù),查詢出來的結(jié)構(gòu)是一個List<Map>集合
先將第一個表的結(jié)果放在一個map集合中。
然后去遍歷第二個結(jié)果的map集合,拿到key,去和第一個map集合的key比較,如果時間正好相等,則將兩個結(jié)果的value相加,最后得到一個總的map集合
將總的map集合的鍵和值分別取出來放在VO類中,最后在前臺遍歷展示。
下面貼一下實(shí)現(xiàn)代碼。
代碼實(shí)現(xiàn)
分別查詢兩個表中的數(shù)據(jù),封裝成List<Map>的形式傳出去。
public?List<Map<String,?Object>>?getFlowsHoursDiSanFangByProvince(Integer?proId){Map<String,Object>?map?=?new?HashMap<String,Object>();List<Map>?mapList?=?new?ArrayList<>();List<Map<String,?Object>>?nameList?=?new?ArrayList<>();String?sql="?SELECT?SUM(acq_num_of_people)?flows,DATE_FORMAT(acq_time,'%H')?AS?hours?FROM?base_disanfang?\n"?+"????????WHERE?DATE(acq_time)?=?CURDATE()\n"?+"??????????AND?province_id?=???\n"?+"????????GROUP?BY??DATE_FORMAT(acq_time,'%H')";Connection?connection?=?null;PreparedStatement?pStatement?=?null;connection?=?getConnection();try?{pStatement?=?connection.prepareStatement(sql);pStatement.setObject(1,proId);ResultSet?rs=pStatement.executeQuery();while(rs.next()){map?=?new?HashMap<String,Object>();map.put("value",rs.getInt(1));map.put("name",rs.getString(2));nameList.add(map);}}?catch?(SQLException?throwables)?{throwables.printStackTrace();}//return?JSONArray.toJSONString(mapList);return?nameList;}public?List<Map<String,?Object>>?getFlowsHoursThreeByProvince(Integer?proId){Map<String,Object>?map?=?new?HashMap<String,Object>();List<Map<String,?Object>>?mapList?=?new?ArrayList<>();List<Map<String,?Object>>?nameList?=?new?ArrayList<>();String?sql="?SELECT?SUM(acq_num_of_people)?flows,DATE_FORMAT(acq_time,'%H')?AS?hours?FROM?base_three?"?+"????????WHERE?DATE(acq_time)?=?CURDATE()?"?+"??????????AND?province_id?=???"?+"????????GROUP?BY??DATE_FORMAT(acq_time,'%H')";Connection?connection?=?null;PreparedStatement?pStatement?=?null;connection?=?getConnection();try?{pStatement?=?connection.prepareStatement(sql);pStatement.setObject(1,proId);ResultSet?rs=pStatement.executeQuery();while(rs.next()){map?=?new?HashMap<String,Object>();map.put("value",rs.getInt(1));map.put("name",rs.getString(2));nameList.add(map);}}?catch?(SQLException?throwables)?{throwables.printStackTrace();}//return?JSONArray.toJSONString(mapList);return?nameList;}開始處理List<Map>結(jié)果集:
//根據(jù)省份編碼獲取小時段的客流信息(日客流趨勢)?basedisanfang的List<Map<String,?Object>>?disanfangLists?=?zhiXingMysql.getFlowsHoursDiSanFangByProvince(proId);//根據(jù)省份編碼獲取小時段的客流信息(日客流趨勢)?basethree的List<Map<String,?Object>>?threeLists?=?zhiXingMysql.getFlowsHoursThreeByProvince(proId);Map<Integer,?Long>?mapTotal?=?new?HashMap<>();//將數(shù)據(jù)放在map集合中for?(Map<String,?Object>?map?:?disanfangLists)?{mapTotal.put(Integer.parseInt(map.get("name").toString()),?Long.parseLong(map.get("value").toString()));}for?(Map<String,?Object>?map?:?threeLists)?{//獲取basethree集合中的鍵int?threekey?=?Integer.parseInt(map.get("name").toString());//獲取mapTotal集合中的鍵,如果有,則相加,如果沒有,則直接用Set?set?=?mapTotal.keySet();for?(Object?hours?:?set)?{//判斷三方的里面有沒有這個鍵if?(Integer.parseInt(hours.toString())?==?threekey)?{//有的話,加起來Long?flows?=?Long.parseLong(map.get("value").toString())?+?mapTotal.get(threekey);//重新添加到map集合中,替換到原來的mapTotal.put(threekey,?flows);}}//獲取basethree的所有鍵Set?basethreeSet?=?map.keySet();Set?mapTotalSet?=?mapTotal.keySet();//遍歷一下看看mapTotal里面有沒有都加進(jìn)去Iterator?ite1?=?mapTotalSet.iterator();Iterator?ite2?=?basethreeSet.iterator();while?(ite2.hasNext())?{if?(mapTotalSet.contains(ite2.next()))?{mapTotal.put(Integer.parseInt(map.get("name").toString()),?Long.parseLong(map.get("value").toString()));}}}Set?mapTotalSet?=?mapTotal.keySet();List<Integer>?hourList?=?new?ArrayList<>();;List<Long>?flowsList?=?new?ArrayList<>();//將鍵和值都放在VO中for?(Object?hours?:?mapTotalSet)?{hourList.add(Integer.parseInt(hours.toString()));flowsList.add(mapTotal.get(hours));}HomeVO類的字段如下:
/***?@author:?muxiongxiong*?@date:?2021年11月27日?下午?1:31*?公眾號:雄雄的小課堂*?博客:https://blog.csdn.net/qq_34137397*?個人站:http://www.穆雄雄.com*?個人站:http://www.muxiongxiong.cn*?@Description:?首頁大屏的封裝類*/ public?class?HomeVO?{//客流排行private??List<Map<String,?Object>>?flowOrderByList?=?new?ArrayList<>();//場館客流占比private??List<Object>?flowZhanBiByDept?=?new?ArrayList<>();//月客流趨勢private??List<BussFlowList>?flowQuShiByMonth?=?new?ArrayList<>();//日客流趨勢(暫時沒有用)private???Map<Integer,?Long>?flowQuShiByDays?=?new?HashMap<>();//客流前五的場館信息private?List<Object>?flowNameOrderFive?=?new?ArrayList<>();//小時客流趨勢的小時private?List<Integer>?hoursList?=?new?ArrayList<>();//小時客流趨勢的客流private?List<Long>?flowsList?=?new?ArrayList<>();public?List<Map<String,?Object>>?getFlowOrderByList()?{return?flowOrderByList;}public?void?setFlowOrderByList(List<Map<String,?Object>>?flowOrderByList)?{this.flowOrderByList?=?flowOrderByList;}public?List<Object>?getFlowZhanBiByDept()?{return?flowZhanBiByDept;}public?void?setFlowZhanBiByDept(List<Object>?flowZhanBiByDept)?{this.flowZhanBiByDept?=?flowZhanBiByDept;}public?List<BussFlowList>?getFlowQuShiByMonth()?{return?flowQuShiByMonth;}public?void?setFlowQuShiByMonth(List<BussFlowList>?flowQuShiByMonth)?{this.flowQuShiByMonth?=?flowQuShiByMonth;}public?Map<Integer,?Long>?getFlowQuShiByDays()?{return?flowQuShiByDays;}public?void?setFlowQuShiByDays(Map<Integer,?Long>?flowQuShiByDays)?{this.flowQuShiByDays?=?flowQuShiByDays;}public?List<Object>?getFlowNameOrderFive()?{return?flowNameOrderFive;}public?void?setFlowNameOrderFive(List<Object>?flowNameOrderFive)?{this.flowNameOrderFive?=?flowNameOrderFive;}public?List<Integer>?getHoursList()?{return?hoursList;}public?void?setHoursList(List<Integer>?hoursList)?{this.hoursList?=?hoursList;}public?List<Long>?getFlowsList()?{return?flowsList;}public?void?setFlowsList(List<Long>?flowsList)?{this.flowsList?=?flowsList;} }最后是前端封裝到echarts中的代碼,因?yàn)?#xff0c;如果現(xiàn)在是17點(diǎn)的話,則17點(diǎn)之后的數(shù)據(jù)應(yīng)該為0才對,所以我這邊的思路是先構(gòu)建一個0-24小時的時間集合,直接扔到面積圖的X軸,y軸則是從VO實(shí)體類中獲取。
這邊還會有個問題,就是查詢的結(jié)果中可能有問題,比如7時沒有數(shù)據(jù),那么返回的結(jié)果就是5 6 8 9 時,沒有7時,所以這個地方還需要進(jìn)行一次與時間遍歷判斷一下,如果取的時間等于當(dāng)前X軸的上的某時,才將數(shù)據(jù)放在Y軸的集合中,否則就填充0。
代碼如下:
//根據(jù)省份獲取每日客流趨勢//this.hoursList?=?null;if?(this.hoursList.length?===?0)?{//構(gòu)造小時for?(let?i?=?0;?i?<?24;?i++)?{this.hoursList.push(i?+?"時");}}this.hoursFlowList?=?new?Array(24).fill(0);for?(var?j?=?0;?j?<?this.homeVo.hoursList.length;?j++)?{//獲取時間,且格式化一下var?shijian?=?this.homeVo.hoursList[j];for?(let?i?=?0;?i?<?this.hoursFlowList.length;?i++)?{if?(shijian?==?i)?{this.hoursFlowList.splice(shijian,?1,?this.homeVo.flowsList[j]);}}}/*這個不能放在括號外面,放外面執(zhí)行的順序不一樣*/this.drawLines("lines",?this.hoursList,?this.hoursFlowList);}) },data中聲明的變量如下:
hoursList:[],???//小時的集合hoursFlowList:[],?//小時段的客流集合homeVo:[],??//返回的數(shù)據(jù)結(jié)果好了,到現(xiàn)在為止,已經(jīng)都實(shí)現(xiàn)了,這種方法可能不是最好的方法,大家也可以說出你們的思路~
總結(jié)
以上是生活随笔為你收集整理的两个map中的数据,按照相同键,将所对应的值相加方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 喜乐安康是什么意思 喜乐安康解释
- 下一篇: publiccms中将推荐页的内容显示在