Java8对数据处理可谓十分流畅,既不改变数据,又能对数据进行很好的处理,今天给大家演示下,用Java8的Stream如何对数据进行分组统计,排序,求和等
汇总统计方法
找到汇总统计的方法。这些方法属于java 8的汇总统计类。
getAverage(): 它返回所有接受值的平均值。
getCount(): 它计算所有元素的总数。
getMax(): 它返回最大值。
getMin(): 它返回最小值。
getSum(): 它返回所有元素的总和。
看官可以根据自己的需求进行灵活变通
- @GetMapping("/list")
- public void list(){
- List<InputForm> inputForms = inputFormMapper.selectList();
- Map<String, IntSummaryStatistics> collect = inputForms.stream()
- .collect(Collectors.groupingBy(InputForm::getCreateUserName, Collectors.summarizingInt(InputForm::getStatus)));
-
- // 对名字去重
- Set<String> collect1 = inputForms.stream().distinct().map(InputForm::getCreateUserName).collect(Collectors.toSet());
-
- // 遍历名字,从map中取出对应用户的status最大值,最小值,平均值。。。
- for (String s1 : collect1) {
- IntSummaryStatistics statistics1 = collect.get(s1);
-
- System.out.println("第一个用户的名字为====" + s1);
- System.out.println("**********************************************");
- System.out.println("status的个数为===" + statistics1.getCount());
- System.out.println("status的最小值为===" + statistics1.getMin());
- System.out.println("status的求和为===" + statistics1.getSum());
- System.out.println("status的平均值为===" + statistics1.getAverage());
- System.out.println();
- System.out.println();
- }
- }
结果如下:
- @GetMapping("/list")
- public void list(){
- List<InputForm> inputForms = inputFormMapper.selectList();
- System.out.println("inputForms = " + inputForms);
-
- Map<String, Long> collect = inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateUserName,
- Collectors.counting()));
-
- System.out.println("collect = " + collect);
- }
其中Collectors.groupingBy(InputForm::getCreateUserName, Collectors.counting())返回的是一个Map集合,InputForm::getCreateUserName代表key,Collectors.counting()代表value,我是按照创建人的姓名进行统计
可以看到总共有九条数据,其中莫昀锦有两个,周亚丽有七个
- @GetMapping("/list")
- public Map<String, List<InputForm>> list(){
- List<InputForm> inputForms = inputFormMapper.selectList();
- System.out.println("inputForms = " + inputForms);
-
- Map<String, List<InputForm>> collect = inputForms.stream()
- .collect(Collectors.groupingBy(InputForm::getCreateCompanyName));
-
- return collect;
- }
- @GetMapping("/list")
- public Map<String, List<InputForm>> list(){
- List<InputForm> inputForms = inputFormMapper.selectList();
- System.out.println("inputForms = " + inputForms);
-
- Optional<InputForm> min = inputForms.stream()
- .min(Comparator.comparing(InputForm::getId));
-
- System.out.println("min = " + min);
- return null;
- }
可以看到此id是最小的,最大值雷同
- @GetMapping("/list")
- public void list(){
- List<InputForm> inputForms = inputFormMapper.selectList();
- System.out.println("inputForms = " + inputForms);
-
- IntSummaryStatistics collect = inputForms.stream()
- .collect(Collectors.summarizingInt(InputForm::getStatus));
- double average = collect.getAverage();
- int max = collect.getMax();
- int min = collect.getMin();
- long sum = collect.getSum();
- long count = collect.getCount();
-
- System.out.println("collect = " + collect);
- }
- // 求最大值
- Optional
max = inputForms.stream().max(Comparator.comparing(InputForm::getAgency)); - if (max.isPresent()){
- System.out.println("max = " + max);
- }
-
- // 求最小值
- Optional
min = inputForms.stream().min(Comparator.comparing(InputForm::getAgency)); - if (min.isPresent()){
- System.out.println("min = " + min);
- }
- int sum = inputForms.stream().mapToInt(InputForm::getStatus).sum();
- System.out.println("sum = " + sum);
- // 求某个字段的平均值
- Double collect2 = inputForms.stream().collect(Collectors.averagingInt(InputForm::getStatus));
- System.out.println("collect2 = " + collect2);
-
- // 简化后
- OptionalDouble average = inputForms.stream().mapToDouble(InputForm::getStatus).average();
- if (average.isPresent()){
- System.out.println("average = " + average);
- }
- // 拼接某个字段的值,用逗号分隔,并设置前缀和后缀
- String collect3 = inputForms.stream().map(InputForm::getCreateUserName).collect(Collectors.joining(",", "我是前缀", "我是后缀"));
- System.out.println("collect3 = " + collect3);
- // 根据部门进行汇总,并获取汇总人数
- Map
collect4 = inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateDeptName, Collectors.counting())); - System.out.println("collect4 = " + collect4);
- // 根据部门和是否退休进行分组,并汇总人数
- Map
> collect5 = inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateDeptName, Collectors.groupingBy(InputForm::getIsDelete, Collectors.counting()))); - System.out.println("collect5 = " + collect5);
- // 根据部门和是否退休进行分组,并取得每组中年龄最大的人
- Map
> collect6 = inputForms.stream().collect( - Collectors.groupingBy(InputForm::getCreateDeptName,
- Collectors.groupingBy(InputForm::getIsDelete,
- Collectors.collectingAndThen(
- Collectors.maxBy(
- Comparator.comparing(InputForm::getAge)), Optional::get))));
- System.out.println("collect6 = " + collect6);