1、沒合並前的數據
合並后的結果:控制台打印,這里已經看到 廣州分部跟佛山分部 已經合成【廣佛分部】
2、Impl(業務層) 上代碼
@Override public List<List<Object>> selectSysCrmDeptGuoGaoCount(String startTime, String endTime) { List<Map<String,Object>> list = countMapper.selectSysCrmDeptGuoGaoCount(startTime, endTime);
//新建一個集合,用於存放 List<Map<String,Object>> newList = new ArrayList<>(); int count = 0; for(Map<String,Object> map : list){
//map.get("dept_name")取出部門 String deptName = (String) map.get("dept_name");
//if判斷過濾出想要的部門數據進行判斷 if(StringUtils.equals("廣州分部",deptName) || StringUtils.equals("佛山分部",deptName)) { String num = map.get("num").toString();
//if識別到部門后,會將num賦值到count中,否則會將沒識別到的部門存放到新建的newList中 count += Integer.valueOf(num); }else{ newList.add(map); } }
//在新建一個Map存儲要替換合並數據,在添加到newList中 Map<String,Object> map = new LinkedHashMap<>(); map.put("dept_name","廣佛分部"); map.put("num",count); newList.add(map);
//Collections.sort(),排序默認sort(),但是要保證集合中的對象是 可比較的。 Collections.sort(newList, Comparator.comparingInt(a -> Integer.valueOf(a.get("num").toString()) )); //降序
Collections.reverse(newList);
return RptUtils.convert(newList); }