stream分组groupingBy和求多少条结果counting
stream分组groupingBy和求多少条结果counting
最近在开发中遇到一个问题,执行 sql 的 group by 操作的时候执行失败, 抛出了 this is incompatible with sql_mode=only_full_group_by 的错误信息,这个错误发生在mysql 5.7 版本以上会出现的问题:,所以就采用了stream的分组。代码如下
List<Userandbook> allSale = this.userandbookService.getAllSale(map); List<Userandbook> data = new ArrayList<>(); //先通过bookid进行分组,然后在通过bookid求结果count Map<Long, List<Userandbook>> collect1 = allSale.stream().collect(Collectors.groupingBy(t -> t.getBookid())); Map<Long, Long> collect = allSale.stream().collect(Collectors.groupingBy(t -> t.getBookid(), Collectors.counting())); Set<Long> strings5 = collect1.keySet(); //循环拿到分组后的count赋值给num返回到前台 for (Long s : strings5) { for (int i = 0; i < collect1.get(s).size(); i++) { collect1.get(s).get(i).setNum(Integer.parseInt(collect.get(s).toString())); } data.add(collect1.get(s).get(0)); }
下一篇:
MATLAB图像处理-特征提取