List<Map<String,Object>> counts = cardMapper.getCount(map);//查詢數據庫獲得的map集合
List<Map<String,Object>> result =new ArrayList<>();
Map<String, List<Map<String, Object>>> glist = counts.stream().collect(Collectors.groupingBy(e -> e.get("card_id").toString()+"#"+e.get("channel_id").toString()+"#"+e.get("store_id")));//對card_id,channel_id,store_id進行分組
//對member_num求和,其餘字段原樣返回glist.forEach((k,slist)->{ Map<String,Object> nmap=new HashMap<>(); IntSummaryStatistics sumcc = slist.stream().collect(Collectors.summarizingInt(e->Integer.valueOf(e.get("member_num").toString()))); nmap.put("card_id", slist.get(0).get("card_id")); nmap.put("channel_id", slist.get(0).get("channel_id")); nmap.put("member_num", sumcc.getSum());//求和 nmap.put("store_id",slist.get(0).get("store_id")); nmap.put("area_id",slist.get(0).get("area_id")); nmap.put("shop_type",slist.get(0).get("shop_type")); result.add(nmap);});