需求中碰到的简单Map集合 key相同合并 value的思路
从两个接口获取到了数据Map集合, 但是要展示到同一页面 根据了播控人为key 将两个返回的进行遍历 将他们存在新的map里面 只有单个key value 就存为(MAP<object,object> ,多个直接存为Map<object,Map>
将两个接口获取到的key作为存入set去重
后去set 根据key(person)去取到各自对应的值,存入map 返回
方法很繁琐 ,循环太多,但遇到了 ,就这么实现的
数据example ::// {(all,xxx),(count,600)},{("张三",xxx),(count,190)}
// {(all,xxx),(count,190),(per,10%)},{("张三,xxx),(count,190),(per,10%)}
@RequestMapping("/sx_review_all") @ResponseBody public List<Map<String, Object>> getReviewData(String startDate, String endDate) { List<Map<String, Object>> list = new ArrayList<>(); //实时数据和离线数据的 List<Map<String, String>> rlData = this.auditorRt(); List<Map<String, Object>> olData = this.auditorOl(startDate, endDate); Map<String, Object> rlmap = new HashMap<>(); Map<String, Map<String,Object>> olmap = new HashMap<>(); Set<String> personSet = new HashSet<>(); ;//存实时播控人 以姓名为key,count为值 for (Map<String, String> rl : rlData) { String person = rl.get("auditor"); rlmap.put(person, rl.get("person_bc_amt")); personSet.add(person); } for (Map<String, Object> ol : olData) { String person = ol.get("bc_person").toString(); Map<String,Object> cacheMap= new HashMap(); cacheMap.put("olAmt",ol.get("count").toString()); cacheMap.put("total",olData.get(0).get("count").toString()); cacheMap.put("is_bc_re", ol.get("is_bc_re").toString()); cacheMap.put("per", ol.get("per").toString()); olmap.put(person, cacheMap); personSet.add(person); } if (olData.size()>0){ for(String person:personSet) { // String person = ol.get("bc_person").toString(); Map<String, Object> cache = new HashMap<>(); cache.put("auditor", person); cache.put("rlAmt", rlmap.get(person) == null ? 0 : rlmap.get(person));//判断 cache.put("olAmt", olmap.get(person) != null ? olmap.get(person).get("olAmt") : 0); cache.put("total", olData.get(0).get("count").toString()); cache.put("is_bc_re", olmap.get(person) != null ? olmap.get(person).get("is_bc_re") : 0); cache.put("per", olmap.get(person) != null ? olmap.get(person).get("per") : 0); list.add(cache); } // return list; } else { for (Map<String, String> rl : rlData) { Map<String, Object> cache = new HashMap<>(); cache.put("auditor", rl.get("auditor")); cache.put("rlAmt", rl.get("person_bc_amt"));//判断 cache.put("olAmt", 0); cache.put("total", 0); cache.put("is_bc_re", 0); cache.put("per", 0); list.add(cache); } } // }else { // if (olData.size() != 0) { // for (Map<String, String> rl : rlData) { // String person = rl.get("auditor"); // Map<String, Object> cache = new HashMap<>(); // cache.put("auditor", rl.get("auditor")); // cache.put("rlAmt", rl.get("person_bc_amt"));//判断 // cache.put("olAmt", olmap.get(person)!=null ?olmap.get(person).get("olAmt"):0); // cache.put("total", olData.get(0).get("count").toString()); // cache.put("is_bc_re",olmap.get(person)!=null ?olmap.get(person).get("is_bc_re"):0 ); // cache.put("per", olmap.get(person)!=null ?olmap.get(person).get("per"):0); // list.add(cache); // } // // return list; // } // // } return list; }