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); }